设计模式—组合模式(十五)

来源:互联网 发布:mac截屏的快捷键是什么 编辑:程序博客网 时间:2024/05/29 02:40

        软件领域中的设计模式的重要性不言而喻。设计模式中运用了面向对象编程语言的重要特性:封装、继承、多态。虽然知道这些特性的定义但是并没有做到真正的理解,这样特性有什么作用?用于什么场合中等等问题,带着疑问开始学习设计模式,主要参考《大话设计模式》和《设计模式:可复用面向对象软件的基础》两本书。

        组合模式(Composite):将对象组合成树形结构以表示‘部分—整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性;

        例如:整体和部分可以被一致对待(如WORD中复制一个文字、一段文字、一篇文章都是一样的操作)


#include <iostream>#include <string>#include <vector>using namespace std;//组合抽象类class Component{public:string m_strName;Component(string strName){m_strName = strName;}virtual void Add(Component* com) = 0;virtual void Display(int nDepth) = 0;};//叶子类——没分支class Leaf : public Component{public:Leaf(string strName) : Component(strName){}virtual void Add(Component* com){cout << "leaf can't add" << endl;}virtual void Display(int nDepth){string strtemp;for (int i = 0; i < nDepth; i++){strtemp += "-";}strtemp += m_strName;cout << strtemp << endl;}};//组合类——有分支class Composite : public Component{private:vector<Component*> m_component;public:Composite(string strName) : Component(strName){}virtual void Add(Component* com){m_component.push_back(com);}virtual void Display(int nDepth){string strtemp;for (int i = 0; i < nDepth; i++){strtemp += "-";}strtemp += m_strName;cout << strtemp << endl;for (Component* p:m_component){p->Display(nDepth + 2);}}};//客户端int main(){Composite* p = new Composite("上海总部");p->Add(new Composite("合肥分公司"));p->Add(new Composite("南京分公司"));Composite* p1 = new Composite("上海财务总部");p1->Add(new Leaf("合肥财务部"));p1->Add(new Leaf("南京财务部"));p->Add(p1);p->Display(1);return 0;}

例:将下图框架用组合模式实现



#include <iostream>#include <string>#include <vector>using namespace std;class Company{protected:string m_strName;public:Company(string strName){m_strName = strName;}virtual void Add(Company* c) = 0;virtual void Display(int nDepth) = 0;virtual void LineOfDuty() = 0;};class ConcreteCompany : public Company{private:vector<Company*> m_company;public:ConcreteCompany(string strName) :Company(strName){}virtual void Add(Company* c){m_company.push_back(c);}virtual void Display(int nDepth){string strtemp;for (int i = 0; i < nDepth; i++){strtemp += "-";}strtemp += m_strName;cout << strtemp << endl;vector<Company*>::iterator p = m_company.begin();while (p != m_company.end()){(*p)->Display(nDepth + 2);p++;}}virtual void LineOfDuty(){vector<Company*>::iterator p = m_company.begin();while (p != m_company.end()){(*p)->LineOfDuty();p++;}}};class HrDepartment : public Company{public:HrDepartment(string strname) : Company(strname){}virtual void Display(int nDepth){string strtemp;for (int i = 0; i < nDepth; i++){strtemp += "-";}strtemp += m_strName;cout << strtemp << endl;}virtual void Add(Company* c){}virtual void LineOfDuty(){cout << m_strName << ":招聘人才" << endl;}};class FinanceDepartment : public Company{public:FinanceDepartment(string strname) : Company(strname){}virtual void Display(int nDepth){string strtemp;for (int i = 0; i < nDepth; i++){strtemp += "-";}strtemp += m_strName;cout << strtemp << endl;}virtual void Add(Company* c){}virtual void LineOfDuty(){cout << m_strName << ":管理财务" << endl;}};//客户端:int main(){ConcreteCompany *p = new ConcreteCompany("北京总公司");p->Add(new HrDepartment("总公司人力资源部"));p->Add(new FinanceDepartment("总公司财务部"));ConcreteCompany *p1 = new ConcreteCompany("上海华东分公司");p1->Add(new HrDepartment("华东人力资源部"));p1->Add(new FinanceDepartment("华东分公司财务部"));p->Add(p1);ConcreteCompany *p2 = new ConcreteCompany("南京办事处");p2->Add(new HrDepartment("南京办事处人力资源部"));p2->Add(new FinanceDepartment("南京办事处财务部"));p1->Add(p2);ConcreteCompany *p3 = new ConcreteCompany("杭州办事处");p3->Add(new HrDepartment("杭州办事处人力资源部"));p3->Add(new FinanceDepartment("杭州办事处财务部"));p1->Add(p3);cout << "结构图:" << endl;p->Display(1);cout << "职责:" << endl;p->LineOfDuty();return 0;}


原创粉丝点击