美文网首页
组合模式

组合模式

作者: 张霸天 | 来源:发表于2017-03-31 15:17 被阅读0次

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

透明方式:在Component中申明所有用来管理子对象的方法。使接口具有一致性。使用起来没区别,但是removeadd没有实现,使用没有意义

安全方式:在Component中不用申明add和remove方法,那么子类的leaf也就不用去实现他,不过由于不透明,所以树叶和树枝不具有相同的接口,客户端调用要做相应的判断,带来不便。



#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>

using namespace std;

inline void coutName(int depth,string name) {
    string dash = "-";
    for (int i = 0; i< depth; i++) {
        cout<<"-";
    }
    cout<<name<<endl;
}

class Company {
protected:
    string name;
    
public:
    Company(string name):name(name) {
        
    }
    
    virtual void Add(Company * c){};
    virtual void Remove(Company * c){};
    virtual void Display(int depth){};
    virtual void LineOfDuty(){};

};

typedef list<Company *> CompanyList;

class ConcreteCompany:public Company {
private:
    CompanyList children ;

public:
    ConcreteCompany (string name):Company(name){}
    void Add(Company * c){ children.push_front(c); };
    void Remove(Company * c){children.remove(c);};
    void Display(int depth){
        cout<< '-' * depth<<"name"<<endl;
        CompanyList::iterator iter;
        
        for (iter = children.begin(); iter != children.end(); iter++) {
            (*iter)->Display(depth + 2);
        }
        
    };
    void LineOfDuty(){
        CompanyList::iterator iter;
        
        for (iter = children.begin(); iter != children.end(); iter++) {
            (*iter)->LineOfDuty();
        }
    }
    
};


class HRDepartment: public Company {
public:
    HRDepartment(string name):Company(name) {}
    void Add(Company * c){  };
    void Remove(Company * c){};
    void Display(int depth){
        coutName(depth,name);
    };
    void LineOfDuty(){
        cout<<name<<"  员工招聘培训管理"<<endl;
    }
};

class FinanceDepartment : public Company {
public:
    FinanceDepartment(string name):Company(name){}
    void Add(Company * c){  };
    void Remove(Company * c){};
    void Display(int depth){
        coutName(depth,name);
    };
    void LineOfDuty(){
        cout<<name<<"  公司财务收支管理"<<endl;
    }
};


void testLesson15(){
    ConcreteCompany * root = new ConcreteCompany("北京总公司");
    root->Add(new HRDepartment("总公司人力"));
    root->Add(new FinanceDepartment("总公司财务"));
    
    ConcreteCompany * comp = new ConcreteCompany("上海分公司");
    comp->Add(new HRDepartment("上海分人力"));
    comp->Add(new FinanceDepartment("上海分财务"));
    root->Add(comp);
    
    
    ConcreteCompany * comp1 = new ConcreteCompany("南京办事处");
    comp1->Add(new HRDepartment("南京办人力"));
    comp1->Add(new FinanceDepartment("南京办财务"));
    comp->Add(comp1);
    
    ConcreteCompany * comp2 = new ConcreteCompany("杭州办事处");
    comp2->Add(new HRDepartment("杭州办人力"));
    comp2->Add(new FinanceDepartment("杭州办财务"));
    comp->Add(comp2);
    
    cout<< "结构图:"<<endl;
    root->Display(1);
    
    cout<<" 指责:"<<endl;
    root->LineOfDuty();
    
}


需求中是体现部分和整体层次的结构时,希望用户可以忽略组合对象与单个对象的不同。统一的使用组合结构中的所有对象,就该考虑组合模式。

相关文章

  • 设计模式:组合模式 职责链模式

    组合模式 职责链模式 组合模式 组合模式将对象组合成树形结构,以表示“部分-整体”的层次结构。 在组合模式的树形结...

  • 第4章 结构型模式-组合模式

    一、组合模式简介 二、组合模式的优缺点 三、组合模式的使用场景 、组合模式的实例

  • 组合模式(统一叶子与组合对象)

    目录 从生活场景出发,映射组合模式 组合模式的理论概念 组合模式的实现 组合模式在源码中的应用 组合 “优于” 继...

  • 组合模式

    1. 组合模式 1.1 组合模式的定义 组合模式(Composite): 又称部分-整体模式, 将对象组合成树形结...

  • 组合模式

    设计模式系列7--组合模式 《Objective-c 编程之道 iOS 设计模式解析》 - 组合模式 常见组合模式...

  • 设计模式 | 组合模式及典型应用

    本文的主要内容: 介绍组合模式 示例 组合模式总结 源码分析组合模式的典型应用java.awt中的组合模式Java...

  • 组合模式

    一、组合模式介绍 二、组合模式代码实例

  • 组合模式

    设计模式之组合模式 什么是组合模式? 组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以...

  • 15、组合模式(Composite Pattern)

    1. 组合模式 1.1 简介   Composite模式,即组合模式,又叫部分整体模式。Composite模式将对...

  • 组合模式原型解析

    组合模式定义: 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象...

网友评论

      本文标题:组合模式

      本文链接:https://www.haomeiwen.com/subject/eirvottx.html