Factory mode definition
Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creation pattern, which provides the best way to create objects.
In factory mode, when creating objects, we do not expose the creation logic to the client, and point to the newly created objects by using a common interface.
As the name suggests, a factory is to create a product. According to whether the product is a specific product or a specific factory, it can be divided into simple factory mode and factory method mode. According to the degree of abstraction of the factory, it can be divided into factory method mode and abstract factory mode. This pattern is used to encapsulate and manage the creation of objects. It is a creation pattern. This paper analyzes the application scenarios, advantages and disadvantages of the three factory modes step by step from a specific example.
Through the factory mode, the right to create product instances is transferred to the factory. We no longer create the objects we need through new, but obtain the products we need through the factory. It reduces the coupling relationship between product users and users
Simple factory mode
Create cars in simple factory mode
/** * automobile */ public interface Car { void create(); } /** *Create BMW */ public class BaomaCar implements Car { public BaomaCar(){ this.create(); } @Override public void create() { System.out.println("create BaoMaCar"); } } /** * Create Mercedes Benz */ public class BenchiCar implements Car { public BenchiCar(){ this.create(); } @Override public void create() { System.out.println("create BenChiCar"); } } /** * Automobile factory */ public class CarFactory { public Car Create(String type){ if("Baoma".equals(type)){ return new BaomaCar(); }else if("Benchi".equals(type)){ return new BenchiCar(); }else{ return null; } } } public class TestFactory { public static void main(String []args){ CarFactory carFactory = new CarFactory(); Car baomaCar = carFactory.Create("Baoma"); Car benchiCar = carFactory.Create("Benchi"); }
3. Factory method
Compared with the simple factory mode in which the factory is responsible for producing all products, the factory method mode distributes the task of generating specific products to specific product factories
That is, an abstract factory is defined, which defines the production interface of products, but is not responsible for specific products. The production tasks are handed over to different derived factories. This eliminates the need to create objects by specifying types.
Factory method pattern creating automobile
/** * Automobile factory */ public interface AbstractFactory { Car createCar(); } /** * BMW automobile factory */ public class BaomaFactory implements AbstractFactory { @Override public Car createCar() { return new BaomaCar(); } } /** * Mercedes Benz automobile factory */ public class BenchiFactory implements AbstractFactory { @Override public Car createCar() { return new BenchiCar(); } } public class TestFactory { public static void main(String []args){ AbstractFactory baomaFactory = new BaomaFactory(); baomaFactory.createCar(); AbstractFactory benchiFactory = new BenchiFactory(); benchiFactory.createCar(); } }
summary
The difference between simple factory and factory method mode is that the behavior of the former to generate products is encapsulated in a method, instantiated according to the type of parameters, and there is no abstract interface. The latter adds an abstract factory to create different products by implementing different factory methods. A method usually corresponds to a product. This method is more scalable than the former. When the demand increases, it fully conforms to the principles of opening and closing and dependency inversion
4. Abstract factory pattern
No matter how the factory splits and abstracts the above two modes, they are only for one kind of product Car. If you want to generate another product Engine, how should you express it?
Abstract factory mode adds an interface for creating products in AbstarctFactory, and realizes the creation of new products in specific sub factories. Of course, the premise is that the sub factories support the production of the products. Otherwise, the inherited interface can do nothing.
/** * engine */ public interface Engine { void create(); } /** * Create Mercedes Benz engine */ public class BenchiEngine implements Engine { public BenchiEngine(){ this.create(); } @Override public void create() { System.out.println("create BenChiEngine"); } } /** * Create BMW engine */ public class BaomaEngine implements Engine { public BaomaEngine(){ this.create(); } @Override public void create() { System.out.println("create BaomaEngine"); } } /** * Mercedes Benz factory */ public class BenchiFactory implements AbstractFactory { @Override public Car createCar() { return new BenchiCar(); } @Override public Engine createEngine() { return new BenchiEngine(); } } /** * BMW factory */ public class BaomaFactory implements AbstractFactory { @Override public Engine createEngine() { return new BaomaEngine(); } @Override public Car createCar() { return new BaomaCar(); } } public class TestFactory { public static void main(String []args){ AbstractFactory baomaFactory = new BaomaFactory(); baomaFactory.createCar(); baomaFactory.createEngine(); AbstractFactory benchiFactory = new BenchiFactory(); benchiFactory.createCar(); benchiFactory.createEngine(); } }
summary
Abstract factory pattern is an upgraded version of factory method pattern. The latter is for a single product, while the former is for a product family. According to the official definition: provide an interface for creating a set of related / interdependent objects without specifying their specific classes.
For example, an automobile factory needs to generate cars, and each car has a series of products such as engines, which means that for each additional car, a new factory needs to be added to provide the realization of new products. At this time, the abstract factory pattern can be used for design. The abstract factory pattern applies to a range of product families