#-*-coding:utf-8-*-
'''
意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
'''
from abc import ABCMeta
class Component():
__metaclass__ = ABCMeta
def __init__(self):
pass
def operation(self):
pass
def add(self, com):
if not isinstance(comp, Component):
raise 'add method first argument shuld be a instance of Component!'
def remove(self, comp):
if not isinstance(comp, Component):
raise 'remove method first argument shuld be a instance of Component!'
def getChild(self, index):
if not isinstance(index, int):
raise 'getChild method first argument should be int type!'
class Composite(Component):
compList = []
def operation(self):
for comp in tuple(self.compList):
comp.operation()
def add(self, comp):
if not isinstance(comp, Component):
raise 'add method first argument shuld be a instance of Component!'
self.compList.append(comp)
def remove(self, comp):
if not isinstance(comp, Component):
raise 'remove method first argument shuld be a instance of Component!'
index = None
# try:
# index = self.compList.index(comp)
# except:
# raise "comp does't exists"
#不用上面的,用index中的except机制,减少try使用耗时间
index = self.compList.index(comp)
if index is not None:
del self.compList[index]
def getChild(self, index):
if not isinstance(index, int):
raise 'getChild method first argument should be int type!'
return self.compList[index]
class Leaf(Component):
def operation(self):
print 'Leaf operation...'
if __name__ == "__main__":
leaf = Leaf()
leaf.operation()
composite = Composite()
composite.add(leaf)
composite.operation()
leaf1 = composite.getChild(0)
leaf1.operation()
'''
意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
'''
from abc import ABCMeta
class Component():
__metaclass__ = ABCMeta
def __init__(self):
pass
def operation(self):
pass
def add(self, com):
if not isinstance(comp, Component):
raise 'add method first argument shuld be a instance of Component!'
def remove(self, comp):
if not isinstance(comp, Component):
raise 'remove method first argument shuld be a instance of Component!'
def getChild(self, index):
if not isinstance(index, int):
raise 'getChild method first argument should be int type!'
class Composite(Component):
compList = []
def operation(self):
for comp in tuple(self.compList):
comp.operation()
def add(self, comp):
if not isinstance(comp, Component):
raise 'add method first argument shuld be a instance of Component!'
self.compList.append(comp)
def remove(self, comp):
if not isinstance(comp, Component):
raise 'remove method first argument shuld be a instance of Component!'
index = None
# try:
# index = self.compList.index(comp)
# except:
# raise "comp does't exists"
#不用上面的,用index中的except机制,减少try使用耗时间
index = self.compList.index(comp)
if index is not None:
del self.compList[index]
def getChild(self, index):
if not isinstance(index, int):
raise 'getChild method first argument should be int type!'
return self.compList[index]
class Leaf(Component):
def operation(self):
print 'Leaf operation...'
if __name__ == "__main__":
leaf = Leaf()
leaf.operation()
composite = Composite()
composite.add(leaf)
composite.operation()
leaf1 = composite.getChild(0)
leaf1.operation()