Simple Factory mode1. What is a simple factory model?
Given an input, a factory instance of the appropriate object, called the Simple Factory mode
2. Gradual introduction of Factory mode
2.1 Xiao Wen to interview, the company asked to use any one of the object of the language to achieve a calculator program, the function is simply two numbers add or subtract, multiply, except
His code is as follows
def main (): num1 = int (input (' The first number is: ')) num2 = Int (input (' The second number is: ')) op = input (' T He operator is (+-*/): ') if op = = ' + ': ret = num1 + num2 elif op = = '-': ret = num1-num2 elif op = = ' * ': ret = num1 * num2 elif op = = '/': ret = num1/num2 return retif __name__ = = ' __main__ ': re t = Main () print (ret) "The first number is:2the second number is:3the operator is (+-*/): +5"
Xiaowen waited for a long time did not receive the company's notice, Xiao Wen's brother Big Wen looked at the code of small text, shook his head:
1. No use of the concept of facing the object
2.if Esle is inefficient, and in extreme cases it affects the efficiency of the program.
3. The case of divisor 0 is not considered
4. Reusability is poor, coupling is too high, should be use object-facing programming to separate business logic from interface logic to reduce coupling
2.2 modified the code
Operation Operation Class:
Class Cal: def __init__ (self, NUM1, num2): self.num1 = num1 self.num2 = num2 def Add (self): return Self.num1 + self.num2 def Sub (self): return self.num1-self.num2 def Mut (self): return self.num1 * Self . num2 def Div (self): if self.num2! = 0: return self.num1/self.num2 else: return ' divisor cannot be 0 '
Client code:
DIC = {' + ': ' Add ', '-': ' Sub ', ' * ': ' Mut ', '/': ' Div ', }def Main (): num1 = int (input (' the first Numbe R is: ') num2 = Int (Input ("The second number is: ')" op = input (' The operator is (+-*/): ') if Hasattr (Cal, D IC[OP]): func = GetAttr (cal, Dic[op]) obj = Cal (Num1, num2) ret = Obj.func () print (ret) if __name__ = = ' __MAIN__ ': Main ()
Da Wen said: Code coupling is still very high, if you add the square root operation, but need to let +-*/to participate in the compilation
Subtraction operations should be separated, modifying one without affecting several other
Increase the idea of facing object integration inheritance
2.3 modified the code:
Operation operations classes and subtraction classes:
Class Cal: def __init__ (self, NUM1, num2): self.num1 = num1 self.num2 = Num2class Add (Cal): def GetResult (self): return self.num1 + self.num2class Sub (Cal): def getresult (self): return SELF.NUM1- Self.num2class Mut (CAL): def getresult (self): return SELF.NUM1 * self.num2class Div (cal): def GetResult (self): if self.num2! = 0: return self.num1/self.num2 else: return ' divisor cannot be 0 '
Xiaowen said: How can I let the calculator know which class I want to use to create the object?
Da Wen said: Today is called you simple Factory mode, you just enter the operation symbol, the factory can instantiate the appropriate object
2.4 wrote the following code
Simple Factory class:
Import Libdic = {' + ': ' Add ', '-': ' Sub ', ' * ': ' Mut ', '/': ' Div ', }class operationfactory: @ Staticmethod def creatobj (operator, NUM1, num2): If Hasattr (Lib, dic[operator]): Class1 = GetAttr (Lib, DIC [operator]) obj = Class1 (num1, num2) return obj
Client code:
def main (): num1 = int (input (' The first number is: ')) num2 = Int (input (' The second number is: ')) op = input (' The operator is (+-*/): ') obj = Operationfactory.creatobj (OP, Num1, num2) ret = obj. GetResult () print (ret) if __name__ = = ' __main__ ': Main ()
Da Wen said: "Later we change the addition algorithm, only need to change the lib.py in the add can be
When we need to add a variety of complex operations, we only need to add the corresponding subclass, and add the corresponding elements in the DIC dictionary, convenient
The radiation in the class factory can find the corresponding class name.
2.5 Finally, let's take a look at some of these classes of structure diagrams:
1.python Implementation Design pattern: Simple Factory mode