composite pattern -- 组合模式

来源:互联网 发布:libreoffice源码 编辑:程序博客网 时间:2024/05/17 23:06

分类: 设计模式 61人阅读 评论(0) 收藏 举报
composite pattern称为组合模式

在面向对象的系统中,我们经常会遇到一类具有"容器"特征的对象,即它们在充当对象的同时,又是其他对象的容器。

“Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.” – GoF

 

举例:

 

在我们常用的windows操作系统中,"文件"的概念大家都知道,不仅可以是文件夹又可以是普通文件。文件夹下面又可放文件夹以及文件,通俗理解是一个树模型。Composite设计模式就是将客户代码与复杂的对象容器结构解耦,让对象容器自己来实现自身的复杂结构,从而使得客户代码就像处理简单对象(文件)一样来处理复杂的对象容器(目录)

实例解析(完成一个树形结构)
有一个文件夹(work)下面有两个文件夹(Daily Record,Key Document)以及2个文件(Address Book,Schedule)。Daily Record下有两个文件(2012-12-18,2012-12-19),Key Document下有一个文件(Meeting document)。
  1 class File  2 {  3     public:  4         File(std::string n)  5         :name(n)  6         {}  7         virtual ~File( ){}  8         virtual void Add(File *s){}  9         virtual int Remove(int n){} 10         virtual File* getChild(int n){}; 11         virtual void Operation( )=0; 12     protected: 13         std::string name; 14 }; 15  16 class Leaf:public File 17 { 18     public: 19         Leaf(std::string n) 20         :File(n) 21         { } 22         void Operation( ); 23 }; 24 void Leaf::Operation( ) 25 { 26     std::cout<<"The leaf is "<<name<<std::endl; 27 } 28  29 class subFile:public File 30 { 31     public: 32         subFile(std::string n) 33         :name(n) 34         {} 35         ~subFile( ); 36         void Add(File *s); 37         int Remove(int n); 38         File * getChild(int n); 39         void  Operation( ); 40     private: 41         std::vector<File*> listFile; 42 }; 43 subFile::~subFile( ) 44 { 45 } 46 void subFile::Add(File *s) 47 { 48      listFile.push_back(s);    49 } 50 int subFile::Remove(int n) 51 { 52     if(n>listFile.size( )||n<0) 53         return -1; 54     listFile.erase(listFile.begin( )+n); 55     return 1; 56 } 57 File* subFile::getChild(int n) 58 { 59     if(n>listFile.size( )||n<0) 60         return NULL; 61     return listFile[n]; 62 } 63 void subFile::Operation( ) 64 { 65     std::vector<File*>::iterator iter=listFile.begin( ); 66     while( iter!=listFile.end( ) ) 67     { 68         //否则编译器会报错 69         std::cout<<"This document is "<<name<<std::endl; 70         (*iter)->  Operation( ); 71         iter++; 72     } 73 } 74  75 int main(int argc,char **argv) 76 { 77     File* f1=new subFile("Work"); 78     File* f2=new subFile("Daily Recond"); 79     File* f3=new subFile("Key Document); 80     File* f4=new Leaf("Address Book"); 81     File* f5=new Leaf("Schedule"); 82     f1->Add(f2); 83     f1->Add(f3); 84     f1->Add(f4); 85     f1->Add(f5); 86     File* f6=new Leaf("2012-12-18"); 87     File* f7=new Leaf("2012-12-19"); 88     f2->Add(f6); 89     f2->Add(f7); 90     File* f8=new Leaf("Meeting Document"); 91     f3->Add(f8); 92     f1->Operation( ); 93     delete f1; 94     delete f2; 95     delete f3; 96     delete f4; 97     delete f5; 98     delete f6; 99     delete f7;100     delete f8;101     return 0;102 }

 

运行结果为

原创粉丝点击