设计模式十二(组合模式,python语言实现)
2012-12-12 09:56
337 查看
基本原理请参考相关书籍。直接给实例
组合模式应用在类似组织结构、目录等自包含结构
本文给出目录管理的例子
运行结果:
组合模式应用在类似组织结构、目录等自包含结构
本文给出目录管理的例子
# -*- coding: utf-8 -*- ####################################################### # # Composite.py # Python implementation of the Class Client # Generated by Enterprise Architect # Created on: 12-十二月-2012 9:06:54 # ####################################################### from __future__ import division from __future__ import print_function from __future__ import unicode_literals from future_builtins import * class Document(object): """This class (a) defines behaviour for components having children, (b) stores child components, and (c) implements child-related operations in the Component interface. """ def __init__(self, name): super(Document,self).__init__() self.name=name pass def Add(self, document): pass def GetChild(self): pass def Operation(self,depth): # forall g in children # g.Operation(); pass def Remove(self, document): pass pass class File(Document): """This class (a) defines behaviour for components having children, (b) stores child components, and (c) implements child-related operations in the Component interface. """ def __init__(self, name): super(File,self).__init__(name) pass def Add(self, document): pass def GetChild(self): pass def Operation(self,depth=1): # forall g in children # g.Operation(); print('-'*depth + self.name) pass def Remove(self, document): pass pass class Folder(Document): """This class (a) defines behaviour for components having children, (b) stores child components, and (c) implements child-related operations in the Component interface. """ m_Document= Document("None") def __init__(self, name): super(Folder,self).__init__(name) self.ls=list() pass def Add(self, document): self.ls.append(document) pass def GetChild(self): print(self.ls) pass def Operation(self,depth=1): # forall g in children # g.Operation(); print('-'*depth + self.name) for child in self.ls: child.Operation(depth+2) pass pass def Remove(self, document): pass pass #客户端 if __name__=="__main__": class Client: """This class manipulates objects in the composition through the Component interface. """ m_Document= Document("None") root= Folder("root") file1=File("file1") root.Add(file1) folder1=Folder("folder1") root.Add(folder1) folder2=Folder("folder2") file2=File("file2") folder2.Add(file2) folder3=Folder("folder3") folder2.Add(folder3) folder1.Add(folder2) root.Operation(1)
运行结果:
相关文章推荐
- 设计模式八(原型模式,python语言实现)
- 设计模式九(单例模式,python语言实现)
- Java设计模式菜鸟系列(十二)组合模式建模与实现
- 设计模式十四(享元模式flyweight,python语言实现)
- 设计模式十八(观察者模式,python语言实现)
- 设计模式十三(外观模式,python语言实现)
- Java设计模式偷跑系列(十二)组合模式建模和实现
- 设计模式十一(桥接模式,python 语言实现)
- 设计模式十九(中介者模式,python语言实现)
- 设计模式十七(状态模式,python语言实现)
- 设计模式十(适配器模式,python语言实现)
- 设计模式 (十二)组合模式(Composite)
- 研磨设计模式解析及python代码实现——(三)适配器模式(Adapter)
- 23种设计模式之python实现--builder模式
- Python实现设计模式--05.建造者模式(Builder Pattern)
- Python设计模式(十二)【代理模式】
- 23种设计模式之python实现--Flyweight模式
- 设计模式----组合模式UML和实现代码
- 使用Python语言写一个简单的KMP模式匹配算法实现
- 设计模式C++实现(7)——外观模式、组合模式