This is different from the simple factory, the simple factory model has only one factory, the factory method model has the corresponding factory for each product
Benefit: Add an operation class (for example, N-Class), only need to increase the operation class and the corresponding factory, two classes, do not need to modify the factory class.
Cons: Adding an operation class will modify the client code, and the factory method simply moves the internal logic judgment of the simple factory to the client.
#!/usr/bin/Env python#-*-coding:utf-8-*-classOperation (Object): """Abstract class"""
def getresult (self): Raise notimplemented
classADD (operation):"""Plus"""
def getresult (self, NUMA, numb):returnNuma +Numb
classOperationfactory (Object): """Factory class"""
def createoperation (self): Raise notimplemented
classaddfactory (operationfactory): def createoperation (self):returnAdd ()
if__name__ = ='__main__': obj=addfactory () ret=obj. CreateOperation () result= Ret. GetResult (2,5) print (result)
Factory design Pattern