您的位置:首页 > 编程语言 > Python开发

Python抽象工厂模式实现

2017-09-03 20:06 155 查看
# -*- coding:utf-8 -*-
'''
Created on 2017.9.3

@author: dell
'''
#注意  python动态的语言 类的属性不一定的被实例所共享
class Item(object):
caption = ''
def __init__(self,caption):
print "Item  caption  "+caption
self.caption = caption

def makeHTML(self):
pass
def getCaption(self):
return self.caption
class Link(Item):
url = ''
def __init__(self,caption,url):
print "Link  caption  "+caption
Item.__init__(self,caption)
self.url = url

def getCaption(self):
return self.caption

class ListLink(Link):
def __init__(self,caption,url):
print "ListLink   caption  "+ caption
Link.__init__(self,caption,url)
def makeHTML(self):
#         print super(ListLink,self).getCaption()

return "<a>"+self.caption+"</a>"

class Factory(object):
def createLink(self,caption,url):
pass

class ListFactory(Factory):
def createLink(self,caption,url):
return ListLink(caption,url)

factory = ListFactory()
l1 = factory.createLink("Hello,Word", "Factory")
print l1.makeHTML()

print l1.__dict__
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: