# -*- 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
网友评论