1. Typical example of a simple factory model is a calculator
def create_operator(op): if op == ‘+‘: return AddOperation() elif op == ‘-‘: return SubOperation() elif op == ‘*‘: return MulOperation() elif op == ‘/‘: return DivOperation()op = create_operator(‘+‘)op.Calc(1, 2)
Components of the Simple factory model
Responsible for the creation of specific product objects, is the core of the model. When the scene is more complex, you may need to use a factory class to create the product.
- Abstract interface or abstract class for a product
Abstract common interfaces for all products
- Specific Product Categories
Concrete implementation classes for abstract interfaces
Disadvantages
- Because the factory class centralizes the creation logic of all instances, it violates the principle of cohesion responsibility allocation and centralizes all creation logic into a factory class.
- The classes it can create can only be considered in advance, and if you need to add new classes, you need to change the factory class.
- As the number of specific product classes in the system increases, there may be a need for factory classes to create different instances based on different conditions. This kind of judgment of the condition and the judgment of the specific product type staggered together, it is difficult to avoid the spread of the module function, the maintenance and expansion of the system is very unfavorable;
Simple Factory mode-python