design pattern--composite pattern 发表于 2010-05-03 | 更新于: 2017-10-12 | | 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134// composite.cpp : Defines the entry point for the console application.//#include<iostream>#include<string>#include<vector>namespace pattern{ /// this pattern is composite pattern. /// in this pattern, it like a tree to call the leaves function. /// so there is an entry of high level (Graph), and each class has a function called "Draw" /// the composite class is "Picture". /// and the leaves are "Line" and "Point". /// This pattern is combine many objects together which have same functions /// and have level structures . /// And the composite class is like a manager. /// It has the same function also. /// Benefits of this pattern is : outer class will never know the composite relationship; /// outer class will deal the composite class only, not all of innerclass. and composite class /// and inner class have same functions or level structures. /// Do you think the composite class like a facade in facade pattern?#ifndef GRAPH_H#define GRAPH_H class Graph{ protected: std::string name; public: Graph(std::string name):name(name){std::cout<<"graph "<<name<<" construct"<<std::endl;} virtual void Draw()=0; virtual ~Graph(){std::cout<< "graph "<<name<< " destroy"<<std::endl;}; };#endif#ifndef PICTURE_H#define PICTURE_H class Picture:public Graph{ private: std::vector<Graph*> list; public: Picture(std::string name):Graph(name){std::cout<<"Picture "<<name<<" construct!"<<std::endl;} virtual void Add(Graph*); virtual void Remove(Graph*); virtual void Draw(); ~Picture(); };#endif Picture::~Picture(){ list.clear(); std::cout<<"Picture destroy!"<<std::endl; } void Picture::Add(pattern::Graph * g){ list.push_back(g); } void Picture::Remove(pattern::Graph *g){ std::vector<Graph*>::iterator i=list.begin(); while(i!=list.end()){ if(g==*i){ list.erase(i); break; } i++; } } void Picture::Draw(){ std::vector<Graph*>::iterator i=list.begin(); while(i != list.end()){ (*i)->Draw(); i++; } }#ifndef SHAPE_H#define SHAPE_H /// Shape class is parent class of "Line" and "Point" class Shape:public Graph{ public: virtual void Draw()=0; Shape(std::string name):Graph(name){std::cout<<"Shape construct!"<<std::endl;} virtual ~Shape(){std::cout<<"Shape destroy!"<<std::endl;} };#endif#ifndef POINT_H#define POINT_H class Point:public Shape{ public: Point(std::string name):Shape(name){std::cout<<"Point construct!"<<std::endl;} ~Point(){std::cout<<"Point destroy!"<<std::endl;} virtual void Draw(); };#endif void Point::Draw(){ std::cout<<"Draw a point!"<<std::endl; }#ifndef LINE_H#define LINE_H class Line: public Shape{ public: Line(std::string name):Shape(name){std::cout<<"Line construct!"<<std::endl;} ~Line(){std::cout<<"Line destroy!"<<std::endl;} virtual void Draw(); };#endif void Line::Draw(){ std::cout<<"Draw a line!"<<std::endl; }}int main(){ pattern::Line l ("line"); pattern::Point p1 ("point 1"); pattern::Point p2("point 2"); pattern::Picture pic("Picture"); pic.Add(&l); pic.Add(&p1); pic.Add(&p2); pic.Draw(); // if you want destroy some shape, remove them first. pic.Remove(&p1); p1.~Point(); pic.Draw(); return 0;} 本文作者: 帐前卒 本文链接: http://chillyc.info/2010/2010-05-03-design-pattern-composite-pattern/ 版权声明: 本博客所有文章除特别声明外,只能复制超链接地址,且必须注明出处!