# 创建型模式-抽象工厂模式 # 在工厂抽象类中有多个创建对象的方法, from abc import ABCMeta, abstractmethod class IApple(metaclass=ABCMeta): @abstractmethod def show(self): pass class HFSApple(IApple): def show(self): print("红富士苹果") class QNJGApple(IApple): def show(self): print("乔纳金果苹果") class IBanana(metaclass=ABCMeta): @abstractmethod def show(self): pass class MBanana(IBanana): def show(self): print("米蕉") class ZMBanana(IBanana): def show(self): print("芝麻蕉") class IStore(metaclass=ABCMeta): @abstractmethod def get_apple(self): pass @abstractmethod def get_banana(self): pass class StoreA(IStore): def get_apple(self): print("商店A的苹果") return HFSApple() def get_banana(self): print("商店A的香蕉") return MBanana() class StoreB(IStore): def get_animal(self): print("商店B的苹果") return QNJGApple() def get_banana(self): print("商店B的香蕉") return ZMBanana() if __name__ == "__main__": store_a = StoreA() animal = store_a.get_banana() animal.show() fruit = store_a.get_apple() fruit.show()
标签:show,创建,self,get,工厂,抽象,print,class,def From: https://www.cnblogs.com/fuchenjie/p/17511563.html