<!-- lang: python --> class Factory: def createFruit(self, fruit): if fruit == "apple": return Apple() elif fruit == "banana": return Banana() class Fruit: def __str__(self): return "fruit" class Apple(Fruit): def __str__(self): return "apple" class Banana(Fruit): def __str__(self): return "banana" if __name__=="__main__": factory = Factory() print factory.createFruit("apple") print factory.createFruit("banana")
工廠模式: (個人理解) 就是有一個工廠類, 這個比較抽象, 它的子類是作具體的事,工廠類經過傳不一樣的參數調用不一樣的子類。(望你們多批評指正)python