抽象工廠模式

用一個抽象類來整合一系列相近的對象,對外只有一個入口。對象

提供一個接口建立相關對象而無需指定他們的實際類。接口

有點對外統一 一個入口,而後更具入口參數實現路由的感受,路由

class PetShop(object):
    def __init__(self,animal_factory=None):
        self.pet_factory=animal_factoryit

    def show_pet(self):
        pet=self.pet_factory()
        print ('we have %s'%pet)
        print ('it say:%s'%pet.speak())class

class Dog(object):
    def speak(self):
        return 'woof'
    def __str__(self):
        return 'Dog'object

class Cat(object):
    def speak(self):
        return 'miaomiao'
    def __str__(self):
        return 'Cat'im


if __name__=='__main__':    
    cat_shop = PetShop(Cat)
    cat_shop.show_pet()
    dog_shop=PetShop(Dog)
    dog_shop.show_pet()
 return

相關文章
相關標籤/搜索