Use simple factory mode for Python Design Mode Programming, python Design Mode
The purpose of the computing mode is to make the code easy to maintain and expand. It cannot be used for the mode. Therefore, a simple tool script does not need to use any mode.
The simple factory model is also called the static factory method model, the simplest of which is the factory model family. The basic working method of this mode: a factory is used to determine which specific product instance to create.
The following is a simple factory example:
def create_animal(name): if name == 'dog': return Dog() elif name == 'cat': return Cat()animal = create_animal('dog')animal.eat('some food')
Create_animal is a factory, and various animals are products. The factory determines what animal products are produced based on the name. The product should have a basic feature. All products produced in the same factory are in a series and have the same functions, such as animals eat food.
The advantage of the simple factory model is that the details of product objects can be encapsulated inside the implementation class. Changing the specific implementation of a product object will not affect other products. High scalability. To add a product type, you only need to add the corresponding implementation class, modify the factory, and add a judgment branch. The risk of modifying factory functions is relatively low.
Another typical example for a simple factory model is a calculator. The calculator must support various computing operations, such as addition, subtraction, multiplication, division, square, cube, square root, and factorial. The factory here is to create a variety of operation algorithm objects, each operation algorithm object supports a value method.
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)
As mentioned above, products produced by each factory are a series. Therefore, product classes are generally derived from the same abstract base class, but they are not necessary.
Elements of the simple factory model:
Factory function: is the core of the model to create specific product objects. When the scenario is complex, a factory class may be required to create the product.
Abstract interfaces or abstract classes of products, public interfaces of all products, specific product classes, and specific implementation classes of abstract interfaces.
Disadvantages:
Because the factory class integrates the creation logic of all instances and violates the High Cohesion responsibility allocation principle, all creation logic is centralized into a factory class.
The classes it can create can only be considered in advance. If you need to add a new class, you need to change the factory class.
When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;
Use Cases:
The factory class is responsible for creating fewer objects;
The customer only knows the parameters passed into the factory class and does not care about how to create objects (logic;
Because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases.
Eg: Implement calculator in simple factory Mode
# Encoding = UTF-8 ## by panda # simple factory mode class OperateBase (): result = 0; def GetResult (self): return self. result; class OperationAdd (OperateBase): def _ init _ (self, NumA, NumB): self. result = NumA + NumB; def GetResult (self): return self. result; class OperationSub (OperateBase): def _ init _ (self, NumA, NumB): self. result = NumA-NumB; def GetResult (self): return self. result; class OperationMult (OperateBase): def _ init _ (self, NumA, NumB): self. result = NumA * NumB; def GetResult (self): return self. result; class OperationDiv (OperateBase): def _ init _ (self, NumA, NumB): self. result = NumA/NumB; def GetResult (self): return self. result; class OperationFactory (): @ staticmethod def createOperate (operate, NumA, NumB): optList = {'+': OperationAdd, '-': OperationSub, '*': OperationMult, '/': OperationDiv,} operator = OperateBase () if (optList. has_key (operate): Operator = optList [operate] (NumA, NumB); return javasdef clientUI (): opt = raw_input ("please input a operation (+ -*/): "); NumA = raw_input (" please input the first number: "); NumB = raw_input (" please input the second number: "); Operation = OperationFactory. createOperate (opt, float (NumA), float (NumB) print "Restlt:", response. getResult () return if _ name _ = '_ main _': clientUI ();
The UML class diagram is as follows:
Articles you may be interested in:
- Measure the test taker's knowledge about the observer mode and Strategy Mode in Python Design Mode Programming.
- Simple program example of interpreter mode in Python Design Mode Programming
- An example of how to use the design pattern in Python
- Analysis of the key points of the Decorator pattern in Python
- Instance parsing Python Design Mode Programming-Application of Bridge Mode
- Examples of Adapter mode in Python Design Mode Programming
- Application Example of prototype in design mode in Python Program
- In-depth analysis of the use of builder mode in Python Design Mode Programming
- An example of how to use the abstract factory mode in Python Design Mode Programming
- The example explains how to use the factory method mode of programming in Python design mode.
- Detailed description of the use of the factory method mode in the design mode in the Python Program
- Example of using the responsibility chain mode and iterator mode in the design mode in Python