The Python Editor interface provides a public interface for processing all personal information. The complete code is shown in List 2. By calling the getUI () method, PIM can obtain the UI components that interact with users. I hope you can learn a lot.
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. In addition, you do not need to modify the client when introducing new products in the system. The disadvantage is that when a new product is to be added to the system, you must modify the factory class 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, designed to make the software architecture as generic as possible.
The abstract parent classes Application and Document are defined. The Python Editor interface 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.
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.
The Python Editor interface defines a common constructive method builder method) getEditor (), which returns an Editor object. The complete code is shown in Listing 1. Any personal information has its own User Interface Editor), which is responsible for obtaining data and modifying it as needed. The only thing PIM has to do is to obtain the Editor through Editable, it is used to perform operations on user input data.
- How to embed Python into C ++ applications?
- In-depth discussion of Ruby and Python syntax comparison
- Introduction to Python
- Python Learning Experience: version, IDE selection and encoding solution
- Analysis of Python GIL and thread security