设计模式十二(组合模式,python语言实现)

基本原理请参考相关书籍。直接给实例

组合模式应用在类似组织结构、目录等自包含结构

本文给出目录管理的例子

 

# -*- 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)

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值