In real life, social division of labor is more and more detailed and specialized. All kinds of products are produced in special factories, which completely say goodbye to the era of self-sufficiency of small-scale agricultural economy, which greatly shortens the production cycle of products and improves production efficiency.
Similarly, can we separate the production and use of software objects in software development?
Definition and characteristics
Definition of factory method pattern: define a factory interface for creating product objects, and postpone the actual creation of product objects to specific sub factory classes.
This meets the requirement of "separation of creation and use" in the creation mode.
We call the created object "product" and the object that creates the product "factory".
If there are not many products to be created, as long as one factory class can be completed. This mode is called "simple factory mode". It does not belong to the 23 classic design modes of GoF. Its disadvantage is that when adding new products, it will violate the "opening and closing principle".
The "factory method mode" introduced in this section is a further abstraction of the simple factory mode. Its advantage is that the system can introduce new products without modifying the original code, that is, to meet the opening and closing principle.
The main advantages of the factory approach model are:
- Users only need to know the name of the specific factory to get the products they want, without knowing the specific creation process of the products;
- When adding new products to the system, only specific product categories and corresponding specific factory categories need to be added, without any modification to the original factory, meeting the opening and closing principle;
Structure and Implementation
The factory method pattern consists of four elements: abstract factory, concrete factory, abstract product and concrete product.
Structure of patterns
The main roles of the factory method pattern are as follows.
- Abstract Factory: provides an interface to create a product, through which the caller accesses the factory method newProduct() of the specific factory to create a product.
- Concrete factory: it mainly implements the abstract methods in the abstract factory and completes the creation of specific products.
- Abstract Product: it defines the Product specification and describes the main features and functions of the Product.
- Concrete product: implements the interface defined by the abstract product role, which is created by the concrete factory. It corresponds to the concrete factory one by one.
class AbstractFactory(object): '''Abstract factory interface''' def new_product(self): ''' //Provides an interface to create a product through which the caller accesses the factory method newProduct() of a specific factory to create a product. :return: AbstractProduct ''' pass class ConcreteFactory1(AbstractFactory): def new_product(self): print("Specific factory 1 --> Specific product 1") return ConcreteProduct1() class ConcreteFactory2(AbstractFactory): def new_product(self): print("Specific plant 2 --> Specific product 2") return ConcreteProduct2() class AbstractProduct(object): '''Abstract product interface''' def show(self): ''' //The product specification is defined, and the main characteristics and functions of the product are described. :return: void ''' pass class ConcreteProduct1(AbstractProduct): def show(self): print("Specific product 1 is produced") class ConcreteProduct2(AbstractProduct): def show(self): print("Specific product 2 produced") if __name__ == '__main__': cf1 = ConcreteFactory1() cf2 = ConcreteFactory2() cp1 = cf1.new_product() cp2 = cf2.new_product() cp1.show() cp2.show()
Specific factory 1 - > specific product 1 Specific factory 2 - > specific product 2 Specific product 1 is produced Specific product 2 produced
Application scenario
The factory method pattern is generally applicable to the following scenarios.
- The customer only knows the name of the factory that created the product, not the specific product name. Such as TCL TV factory, Hisense TV factory, etc.
- The task of creating objects is completed by one of several concrete sub factories, while the abstract factory only provides the interface of creating products.
- Customers don't care about the details of creating products, only about the brand of products.
Pattern extension
When there are not many products to be generated and they will not be added, and a specific factory class can complete the task, the abstract factory class can be deleted. At this time, the factory method pattern will degenerate to the simple factory pattern, and its structure diagram is as shown in the figure.
class Payment(object): '''Abstract product''' def pay(self): pass class Alipay(Payment): def pay(self): print('Alipay paid the money!') class WXpay(Payment): def pay(self): print("WXpay paid the money!") class PayMethod(object): def create_payment(self, pay_method): if pay_method == 'alipay': return Alipay() if pay_method == 'wxpay': return WXpay() if __name__ == '__main__': pm1 = PayMethod() alipay = pm1.create_payment('alipay') alipay.pay() wxpay = pm1.create_payment('wxpay') wxpay.pay()
Alipay paid the money! WXpay paid the money!