Software design masters always know how to design software better than beginners, because they have the magic weapon of design patterns. As an advanced form of software reuse, the design model is the collective wisdom of many outstanding software designers and can guide the software design process.
BKJIA recommended reading: Python design mode: use mode to change Software Design
Factory Method) mode, also known as Virtual Constructor) mode or multi-state Factory Polymorphic Factory mode, belongs to the class creation mode. In the factory method mode, the parent class is responsible for defining the public interfaces for creating objects, while the Child class is responsible for generating specific objects, the purpose is to delay the class instantiation operation to the subclass, that is, the subclass determines which class should be materialized.
In simple factory mode, a factory class is at the center of product class instantiation. It knows the details of each product class and determines when a product class should be instantiated. The advantage of the simple factory mode is that the client can be independent from the product creation process, and there is no need to modify the client when introducing new products to the system. The disadvantage is that when a new product is to be added to the system, the factory class must be modified to add the necessary processing logic. The fatal weakness of the simple factory model is the factory class in the core position, because once it cannot determine which class to instantiate, it cannot be used, the factory method mode can effectively avoid this problem.
Consider such an application Framework), which can be used to browse various formats of documents, such as TXT, DOC, PDF, HTML, etc, in order to make the software architecture as generic as possible, the abstract parent classes "Application" and "Document" are defined at the design stage. The customer must use their subclass to process a specific type of documents. For example, to use this framework to compile a PDF file browser, you must first define two classes, namely Application and Document, which should inherit from Application and Document.
Application is responsible for managing documents and creating them as needed. For example, when you select Open or New from the menu, Application is responsible for creating an Document instance. Obviously, the specific Document subclass to be instantiated is related to a specific Application. Therefore, the Application cannot predict which Document subclass will be instantiated and only knows When a new Document will be created, but I don't know Which) the specific Document will be created. If you still stick to the simple factory mode, there will be a very embarrassing situation: the framework must instantiate the class, but it only knows the abstract class that cannot be instantiated.
The solution is to use the factory method mode, which encapsulates the information to be created by the Document subclass and can separate the information from the framework. 1. The subclass of Application redefined the abstract method createDocument () of Application and returned an instance of an appropriate Document subclass. CreateDocument () is a factory method, because it vividly describes the class instantiation process, that is, it is responsible for "producing" an object.
To put it simply, the function of the factory method mode is to generate various types of instances based on different conditions. These instances generally belong to multiple similar types and share a common parent class. The factory method mode encapsulates the creation process of these instances, which simplifies the compilation of customer programs and improves the scalability of the software architecture, this allows you to add new child classes at minimal cost in the future. The factory method is applicable to the following scenarios:
◆ When you cannot know which class the object to be created belongs to or which class the object will be returned, the premise is that these objects comply with certain interface standards.
◆ When a class wants its subclass to determine the object to be created, its purpose is to make the program more scalable and more flexible when it is added to other classes.
◆ When the responsibility of creating an object is delegated to one of the multiple help sub-classes helper subclass), and the sub-classes that you want to localize are proxies.
It should be noted that using the factory method to create an object does not necessarily mean that the Code will be shorter, but it is often longer), and more auxiliary classes may need to be designed, however, it can indeed flexibly and elastically create unconfirmed objects, which simplifies the logic structure of the client application and improves the readability and reusability of the Code.