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

设计模式十二(组合模式,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)

运行结果:

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