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


[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.     www.2cto.com
    """ 
    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) 
 

相关内容

    暂无相关文章

评论关闭