catalogue
summary
The factory pattern separates the creator from the caller, shields and isolates the specific process of creating objects, and achieves the purpose of improving flexibility.
No factory mode
There is no factory dedicated to creating objects. The creation of objects is mixed with the invocation of objects.
/* * No factory mode */ interface Car {//The relationship between car, Audi and BYD is actually more like inheritance, but here is only an example void run(); } class Audi implements Car { @Override public void run() { System.out.println("Audi is running"); } } class BYD implements Car { @Override public void run() { System.out.println("BYD is running"); } } public class NoFactoryDemo { public static void main(String[] args) { Car a = new Audi(); Car b = new Audi(); a.run(); b.run(); } }
Simple factory mode
Define a factory class for creating objects.
/* * Simple factory mode */ interface Car { void run(); } class Audi implements Car { @Override public void run() { System.out.println("Audi is running"); } } class BYD implements Car { @Override public void run() { System.out.println("BYD is running"); } } //Factory class class CarFactory { // Mode 1 public static Car getCar(String type) { if ("audi".equals(type)) { return new Audi(); } else if ("BYD".equals(type)) { return new BYD(); } else { return null; } } // Mode II public static Car getAudi() { return new Audi(); } public static Car getBYD() { return new BYD(); } } //caller public class Simple { public static void main(String[] args) { Car a = CarFactory.getCar("audi"); a.run(); Car b = CarFactory.getCar("BYD"); b.run(); } }
shortcoming
For adding new products, it is impossible to expand without modifying the code. Violation of the opening and closing principle (release for extension; close for modification).
Factory method model
Relative to a simple factory, the factory method pattern has a set of factory classes that implement the same interface. In this way, the pressure on several re factory methods in the simple factory mode can be shared by different factory subclasses in the factory method mode.
/* * Factory method model * */ interface Car { void run(); } class Audi implements Car { @Override public void run() { System.out.println("Audi is running"); } } class BYD implements Car { @Override public void run() { System.out.println("BYD is running"); } } // Factory interface interface Factory { Car getCar(); } // Two factory classes class AudiFactory implements Factory { @Override public Car getCar() { return new Audi(); } } class BYDFactory implements Factory { @Override public Car getCar() { return new BYD(); } } // caller public class FactoryMethod { public static void main(String[] args) { Car a = new AudiFactory().getCar(); Car b = new BYDFactory().getCar(); a.run(); b.run(); } }
summary
Simple factory mode and factory method mode really avoid code changes?
No, In the simple factory mode, the judgment statement in the factory role should be modified for the addition of new products; In the factory method pattern, either the judgment logic is left in the abstract factory role, or the specific factory role is written dead in the client program (as in the above example). Moreover, the modification of product object creation conditions will inevitably lead to the modification of factory roles.
Facing this situation, the ingenious combination of Java reflection mechanism and configuration file breaks through the limitation - which is perfectly reflected in the Spring framework.
Abstract factory pattern
The difference between the abstract factory pattern and the factory method pattern lies in the complexity of creating objects. Moreover, the abstract factory pattern is the most abstract and general of the three patterns.
The purpose of the abstract factory pattern is to provide an interface for the client to create product objects in multiple product families.
In addition, the following conditions must be met to use the abstract factory pattern:
- There are multiple product families in the system, and the system can only consume one product at a time.
- Products belonging to the same product family are used by them.