Pythonデザインモード単純工場(デザインモードの禅)

1696 ワード

# -*- coding: utf-8 -*-
# author: zhonghua
# filename: pd_factory.py
# create: 2016/3/28
# version: 1.0

'''       
       《      》   
'''

class Human:
    def get_color(self):
        pass

    def talk(self):
        pass

class BlackHuman(Human):
    def get_color(self):
        print '             !'

    def talk(self):
        print '     ,      。'

class YellowHuman(Human):
    def get_color(self):
        print '             !'

    def talk(self):
        print '       ,         。'

class WhiteHuman(Human):
    def get_color(self):
        print '             !'

    def talk(self):
        print '       ,         。'

class AbstractHumanFactory:
    def create_human(self, c):
        pass

class HumanFactory(AbstractHumanFactory):
    def create_human(self, c):
        human = None
        if c == 'black':
            return BlackHuman()
        elif c == 'yellow':
            return YellowHuman()
        elif c == 'white':
            return WhiteHuman()
        else:
            return

if __name__ == '__main__':
    yinyanglu = HumanFactory()

    print '      .....'
    whitehuman = yinyanglu.create_human('white')
    whitehuman.get_color()
    whitehuman.talk()
    print '*'*20

    print '      .....'
    yellowhuman = yinyanglu.create_human('yellow')
    yellowhuman.get_color()
    yellowhuman.talk()
    print '*'*20

    print '      .....'
    blackhuman = yinyanglu.create_human('black')
    blackhuman.get_color()
    blackhuman.talk()
    print '*'*20