It is also called static factory mode. In fact, it does not belong to 23 design modes.
His realization is mainly
Define a class that dynamically determines which instance of a product class (which inherits from a parent class or interface) should be created according to the passed in parameters. In fact, the instantiation of a specific class is handed over to the static method of a class for execution (which reflects the separation of creator and caller). This class is called factory class, and the static method is called static factory method.
First, take buying a car as an example to create a familiar process
Create an interface first:
public interface Car { //Property with a name String name(); }
Then create two entity classes:
Tesla
public class Tesla implements Car{ @Override public String name() { return "Tesla"; } }
Wuling
public class WuLing implements Car{ @Override public String name() { return "Wuling"; } }
Create consumers to buy these two types of cars
public class Customer { public static void main(String[] args) { //Then as a consumer, if you need to buy a Wuling and a Tesla //You need to create these two instance objects Car car1=new WuLing(); Car car2=new Tesla(); System.out.println(car1.name()); System.out.println(car2.name()); } }
However, there is a big disadvantage in this basic mode, that is, when instantiating an object, you need a new one, just like when buying a car, you need to create your own new car, so you need to have a factory class
Pattern diagram
Simple factory mode:
CarFactory
public class CarFactory{ //The simple static factory class is new in this place //Write a static method in this class that can be called directly public static Car getCar(String name){ //But I don't know if it's abnormal, that is, if the entered car name is different if(name.equals("Tesla")){ //Then create Tesla's car return new Tesla(); }else if(name.equals("Wuling")){ return new WuLing(); } //Here is to prevent some new cars without names return new Car() { @Override public String name() { return "unknown"; } }; } }
FactoryCustomer
public class FactoryCustomer { public static void main(String[] args) { //Here is the factory //Create this factory class first Car car1=CarFactory.getCar("Tesla"); Car car2=CarFactory.getCar("Wuling"); Car car3=CarFactory.getCar("anonymous person"); System.out.println(car1.name()); System.out.println(car2.name()); System.out.println(car3.name()); } }
However, this simple factory mode does not conform to the opening and closing principle, that is, when we have a new car, we need to add if judgment on the CarFactory side, so the simple factory mode is also a static factory mode. Not suitable for dynamic increase
Model diagram:
Factory method mode:
In fact, it is simply to define a factory interface, and then implement the factory interface for each vehicle to become its own car factory
Create Car interface first:
public interface Car { //Property with a name String name(); }
Tesla and WuLing are two implementation classes:
public class Tesla implements Car { @Override public String name() { return "Tesla"; } }
public class WuLing implements Car { @Override public String name() { return "Wuling"; } }
CarFactoryu interface
public interface CarFactory { //This interface has a method Car getCar(); }
Tesla factory interface and WuLing factory interface:
public class WuLingFactory implements CarFactory{ @Override public Car getCar() { return new WuLing(); } }
public class TeslaFactory implements CarFactory{ @Override public Car getCar() { //Implement the factory interface and create a corresponding class return new Tesla(); } }
Finally, the customer class
public class Customer { public static void main(String[] args) { //So you need to connect with the corresponding factory? Anyway, the purpose is not to need new? //Is this unnecessary //But in this way, new can be understood as that a new car is equivalent to building a car, but a new factory is equivalent to finding a 4s store... TeslaFactory teslaFactory=new TeslaFactory(); WuLingFactory wuLingFactory=new WuLingFactory(); Car car1=teslaFactory.getCar(); Car car2=wuLingFactory.getCar(); System.out.println(car1.name()); System.out.println(car2.name()); } }
The advantage of this is that it meets the opening and closing principle and does not need to be modified. However, you need to add each time: