1, Introduction to factory mode
Factories are usually used to mass produce goods. Goods produced by factories can often mass produce the same goods.
In software development, if an object needs to be created, the object can be regarded as a product, and the object that creates the product is called a factory. In this way, the creation of objects and the use of objects can be decoupled. The object references do not need to be related to how the objects are created, and the factory can ensure that the created objects are the same.
Definition of factory pattern: define a factory interface for creating objects, and hand over the actual creation of product objects to the factory class or subclass of the factory class for implementation.
According to the actual business scenario, the factory pattern can be divided into three patterns: simple factory pattern, factory method pattern and abstract factory pattern. Both factory method pattern and abstract factory pattern are one of GoF design patterns
2, Simple factory mode
When the product type created is known and limited, it often needs only one factory class. At this time, it is the simple factory mode.
The simple factory pattern has a specific factory class that can create different types of products. It usually provides a static method to obtain objects and create objects of the specified type according to the passed in parameters. Therefore, it can also be called the static factory method pattern.
Although the simple factory mode is simple to implement, it does not have strong scalability. Every time a product type is added, a specific product class needs to be added and the corresponding specific factory class needs to be modified. The expansion cost is large and violates the opening and closing principle
2.2 role of simple factory model
Simple factory: responsible for creating classes for all products
Abstract object: the abstraction of all objects created by a simple factory. It can be an abstract parent class or an interface
Specific products: specific object types created by simple factories
2.2. Simple factory model case
Auto factories can create cars of different brands. At this time, you can define a factory to create cars, and create car objects of different brands according to the incoming car brands
Code implementation:
Define car abstract classes
/** * Automobile interface */ public interface Car { public void showCarBrand(); }
Define the car category of specific brand
/** * Mercedes Benz products */ public class BenzCar implements Car{ @Override public void showCarBrand() { System.out.println("I'm a Mercedes"); } }``` ```java /** * BMW product type */ public class BmwCar implements Car{ @Override public void showCarBrand() { System.out.println("I'm a BMW"); } }
/** * Audi products */ public class AudiCar implements Car{ @Override public void showCarBrand() { System.out.println("I'm an Audi"); } }
Define automobile factory class
/** * Create factory classes for automotive products */ public class CarFactory { public static Car getCar(String brand){ Car car = null; switch (brand){ case "benz": car = new BenzCar(); break; case "bmw": car = new BmwCar(); break; case "audi": car = new AudiCar(); break; default: System.out.println("Unknown brand"); break; } return car; } }
Test code
public static void main(String[] args) throws CloneNotSupportedException { Car benz = CarFactory.getCar("benz"); Car audi = CarFactory.getCar("audi"); benz.showCarBrand(); audi.showCarBrand(); }
I'm a Mercedes I'm an Audi
3, Factory method model
Although the simple factory mode is simple, it has poor scalability and violates the opening and closing principle. The factory method mode is a further abstraction of the simple factory mode. It can expand and create more types of products without modifying the factory, that is, it meets the opening and closing principle
In the simple factory mode, the creation of objects is implemented by the factory itself, while in the factory method mode, the factory is only responsible for providing factory objects of specific products, not directly creating products.
3.1. Role in factory method mode
Abstract factory: it provides an abstraction for creating products. The caller creates objects by abstracting the factory methods that access the specific factory
Concrete factory: implement the abstract method of the abstract factory to complete the creation of specific products
Abstract object: the abstraction of all objects created by a simple factory. It can be an abstract parent class or an interface
Specific products: specific object types created by simple factories
3.2. Factory method mode case
Automobile factories are becoming larger and larger, and the manufacturing of different types of automobiles is becoming more and more specialized. Therefore, automobile manufacturers of different brands are built separately, and the General Factory is responsible for providing basic specifications for each specific sub factory
Code implementation:
Based on the case code in 3.1, modify CarFactory, define it as an abstract interface, and define a method to create automobile products
/** * Create factory interfaces for automotive products */ public interface CarFactory { /** Define the method of obtaining automobile products */ public Car getCar(); }
public class BenzCarFactory implements CarFactory{ @Override public Car getCar() { System.out.println("Mercedes Benz factory creates Mercedes Benz cars"); return new BenzCar(); } }
public class AudiCarFactory implements CarFactory{ @Override public Car getCar() { System.out.println("Audi produces Audi cars"); return new AudiCar(); } }
Test code:
public static void main(String[] args) throws CloneNotSupportedException { CarFactory carFactory = new BenzCarFactory(); Car benz = carFactory.getCar(); benz.showCarBrand(); }
1 Mercedes Benz factory creates Mercedes Benz cars 2 I'm a Mercedes
For the caller, it does not need to care about how the specific automobile product is created, but only need to obtain the specific object of the specified type from the factory. In this way, the decoupling between the referent class and the specific product class is realized, and the Dimitri rule in the design pattern is followed.
4, Abstract factory pattern
The definition of abstract factory pattern is a pattern structure that provides an interface for an access class to create a group of related or interdependent objects, and the access class can obtain different levels of products of the same family without specifying the specific class of the product.
In the factory method mode, each specific factory class can only create the same type. For example, Audi factory can only produce Audi cars and Mercedes Benz factory can only produce Mercedes Benz cars. But in reality, factories usually do not produce only one product, but many kinds of products. For example, in addition to making Mercedes Benz cars, Mercedes Benz factories can also produce sports cars and commercial vehicles.
Abstract factory pattern is an upgraded version of factory method pattern. Factory method pattern only provides factories that produce one product, while abstract factory pattern can produce multiple kinds of products.
Advantages and disadvantages of abstract factory mode:
1. The multi-level products associated with the product family can be managed together within the class. It is not necessary to introduce multiple new classes for management
2. Abstract factory enhances the scalability of the program. When adding a new product family, there is no need to modify the original code and meet the opening and closing principle
3. When adding new products to a product family, all factory classes need to be modified, which is difficult to abstract and understand
4.1 role of abstract factory pattern
Abstract factory: it provides an interface for creating products, including multiple methods for creating products, and can create multiple different products
Concrete factory: implement multiple abstract methods of abstract factory to complete the creation of specific products
Abstract object: the abstraction of all objects created by a simple factory. It can be an abstract parent class or an interface
Specific products: specific object types created by simple factories
It can be found that the roles in the abstract factory mode and the factory method mode are basically the same. The difference is that the abstract factory not only defines the creation method of one product, but also defines the creation method of multiple products, and the specific factory also needs to implement the creation method of multiple products
4.2 cases of abstract factory mode
In addition to producing cars, different automobile factories have expanded other products. For example, Mercedes Benz factory can also produce Mercedes Benz engines, and Audi factory can produce Audi engines
Code implementation:
Define engine product interface
/** * Define engine model */ public interface Engine { public void showEngineBrand(); }
Define engine interface implementation classes, such as Mercedes Benz engine
public class BenzEngine implements Engine{ @Override public void showEngineBrand() { System.out.println("I'm a Mercedes Benz engine"); } }
Modify the abstract factory class based on the 3.2 case code to produce not only cars but also engines
/** * Create factory interface for automotive product family */ public interface CarFactory { /** Define the method of obtaining automobile products */ public Car getCar(); /** Define the method to get the engine */ public Engine getEngine(); }
Specific product factories realize abstract factories. For example, Mercedes Benz factories need to create Mercedes Benz cars and Mercedes Benz engines
public class BenzCarFactory implements CarFactory{ @Override public Car getCar() { System.out.println("Mercedes Benz factory creates Mercedes Benz cars"); return new BenzCar(); } @Override public Engine getEngine() { System.out.println("Mercedes Benz factory creates Mercedes Benz engine"); return new BenzEngine(); } }
Test code:
public static void main(String[] args) throws CloneNotSupportedException { CarFactory carFactory = new BenzCarFactory(); Car benz = carFactory.getCar(); Engine engine = carFactory.getEngine(); benz.showCarBrand(); engine.showEngineBrand(); }
1 Mercedes Benz factory creates Mercedes Benz cars 2 Mercedes Benz factory creates Mercedes Benz engine 3 I'm a Mercedes 4 I am a Mercedes Benz engine
It can be seen that compared with the factory method mode, the abstract factory mode enriches the product types of the factory and avoids creating different factories for different products. Instead, the products of the same ethnic group are created by the same factory, which reduces the number of classes and complexity
However, the abstract factory is not better than the factory method pattern, which mainly depends on the business expansion needs in the later stage.
1. When a specific factory needs to be added for business expansion, such as Audi factory and Porsche factory from Mercedes Benz factory expansion, there is no need to modify the abstract factory to meet the opening and closing principle
2. When a specific product needs to be added for business expansion, such as adding production engines, production wheels and production fuel tanks from the production of cars in Mercedes Benz factory, the abstract factory needs to be modified, which may affect other specific factories. At this time, the opening and closing principle is not satisfied
Therefore, which factory mode should be used in the specific business scenario depends on the later business expansion requirements, but the opening and closing principles should be met as much as possible.