# 创建型模式-工厂方法模式 # 相对于简单工厂模式,如果添加了新的动物类,对用的工厂方法需要一并修改违反了开闭原则, # 因此工厂方法模式, 新增了抽象工厂类,将具体工厂进行隔离满足开闭原则 from abc import ABCMeta, abstractmethod class IAnimal(metaclass=ABCMeta): @abstractmethod def say(self): pass class Dog(IAnimal): def say(self): print("汪汪") class Cat(IAnimal): def say(self): print("喵喵") class IFactory(metaclass=ABCMeta): animal: IAnimal = None @abstractmethod def get_animal(self): pass class DogFactory(IFactory): def get_animal(self): return Dog() class CatFactory(IFactory): def get_animal(self): return Cat() if __name__ == "__main__": cat_factory = CatFactory() animal = cat_factory.get_animal() animal.say()
标签:创建,self,模式,工厂,animal,class,def From: https://www.cnblogs.com/fuchenjie/p/17511260.html