Python实现Factory Method模式
#-*-coding:utf-8-*-
'''
1,从一些产品中提取出共同的属性和方法,组建为基类
再通过继承基类来实现具体的产品类
2,定义工厂接口,
'''
from abc import ABCMeta, abstractmethod, abstractproperty
class Product():
__metaclass__ = ABCMeta #使得Product为抽象基类
def __init__(self):
pass
def say(self):
print 'I have a dream!'
class ConcreteProduct(Product):
def say(self):
print 'I want to travel!'
class Factory():
__metaclass__ = ABCMeta
def __init__(self):
pass
def createProduct(self):
pass
class ConcreteFactory(Factory):
def createProduct(self):
return ConcreteProduct()
#创建一类产品
'''
为一类对象提供创建接口或延迟对象的创建到子类中实现
'''
if __name__ == "__main__":
cf1 = ConcreteFactory()
cp1 = cf1.createProduct()
cp1.say()