结构型模式-组合(composite)

组合

将对象组合成树形结构以表示“部分-整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性.

实例

main.cc:

#include <windows.h>
#include "shop_saler.h"
#include "shop_keeper.h"
#include <iostream>
using namespace std;

/*
design_pattern:"compositor"
        总 店 长(A)
        /    |    \
        B    C     D
                    /     \
                   E       F
                   / \ 
                  G   H
A:深圳总店长(熊总)                             
B:总店店员(李一)                              
C:总店店员(王二)
D:宝安区店长(张三)
E:宝安区店员(李四)
F:宝安区西乡街道店长(刘五)
G:西乡街道店员(陈六)
H:西乡街道店员(方七)
*/
int main(){
    Employee *shop_keeper_A = new ShopKeeper("深圳总店长(熊总)",40,10000);
    Employee *shop_saler_B = new ShopSaler("总店店员(李一)",18,6000);
    Employee *shop_saler_C = new ShopSaler("总店店员(王二)",19,5500);
    Employee *shop_keeper_D = new ShopKeeper("宝安区店长(张三)",35,8000);
    Employee *shop_saler_E = new ShopSaler("宝安区店员(李四)",20,5000);
    Employee *shop_keeper_F = new ShopKeeper("宝安区西乡街道店长(刘五)",33,7000);
    Employee *shop_saler_G = new ShopSaler("西乡街道店员(陈六)",21,4500);
    Employee *shop_saler_H = new ShopSaler("西乡街道店员(方七)",22,4000);

    //From bottom to top
    shop_keeper_F->Add(shop_saler_G);
    shop_keeper_F->Add(shop_saler_H);
    shop_keeper_D->Add(shop_saler_E);
    shop_keeper_D->Add(shop_keeper_F);
    shop_keeper_A->Add(shop_saler_B);
    shop_keeper_A->Add(shop_saler_C);
    shop_keeper_A->Add(shop_keeper_D);
    shop_keeper_A->ShowInformation();

    //clear
    delete shop_keeper_A;
    delete shop_saler_B;
    delete shop_saler_C;
    delete shop_keeper_D;
    delete shop_saler_E;
    delete shop_keeper_F;
    delete shop_saler_G;
    delete shop_saler_H;
    system("Pause");
    return 0;
}

Employee:

//employee.h
#ifndef HELENDP_SOURCE_EMPLOYEE_H_
#define HELENDP_SOURCE_EMPLOYEE_H_
#include <string>
using namespace std;

class Employee{
public:
    Employee(string name,int age,int salary);
    virtual ~Employee();
    string GetName();
    virtual void Add(Employee *employee);
    int GetAge();
    int GetSalary();
    virtual void ShowInformation();
private:
    string name_;
    int age_;
    int salary_;
};
#endif


//employee.cc
#include "employee.h"
#include <iostream>
using namespace std;

Employee::Employee(string name,int age,int salary){
    name_ = name;
    age_ = age;
    salary_ = salary;
}

Employee::~Employee(){

}

void Employee::Add(Employee * employee){
    cout << "Employee::Add: will not run here" << endl;
}

void Employee::ShowInformation(){
    cout << "Employee::ShowInformation: will not run here" << endl;
}
string Employee::GetName(){
    return name_;
}

int Employee::GetAge(){
    return age_;
}

int Employee::GetSalary(){
    return salary_;
}

ShopSaler:

//shop_saler.h
#ifndef HELENDP_SOURCE_SHOP_SALER_H_
#define HELENDP_SOURCE_SHOP_SALER_H_
#include "employee.h"

class ShopSaler : public Employee{
public:
    ShopSaler(string name,int age,int salary);
    ~ShopSaler();
    void Add(Employee *employee);
    void ShowInformation();
};
#endif


//shop_saler.cc
#include "shop_saler.h"
#include <iostream>
using namespace std;

ShopSaler::ShopSaler(string name,int age,int salary):Employee(name,age,salary){

}

ShopSaler::~ShopSaler(){

}

void ShopSaler::Add(Employee *employee){
    cout << "ShopSaler::Add: will not run here" << endl;
}

void ShopSaler::ShowInformation(){
    cout << this->GetName() << "," << this->GetAge() << "," << this->GetSalary() << endl;
}

ShopKeeper:

//shop_keeper.h
#ifndef HELENDP_SOURCE_SHOP_KEEPER_H_
#define HELENDP_SOURCE_SHOP_KEEPER_H_
#include "employee.h"
#include <list>
using namespace std;

class ShopKeeper : public Employee{
public:
    ShopKeeper(string name,int age,int salary);
    ~ShopKeeper();
    void Add(Employee *employee);
    void ShowInformation();
private:
    list<Employee *> list_employee_;
};
#endif


//shop_keeper.cc
#include "shop_keeper.h"
#include <iostream>
using namespace std;

ShopKeeper::ShopKeeper(string name,int age,int salary) : Employee(name,age,salary){
}

ShopKeeper::~ShopKeeper(){

}

void ShopKeeper::Add(Employee *employee){
    list_employee_.push_back(employee);
}

void ShopKeeper::ShowInformation(){
    cout << this->GetName() << "," << this->GetAge() << "," << this->GetSalary() << endl;
    for(list<Employee *>::iterator v = list_employee_.begin();v != list_employee_.end();v++){
        (*v)->ShowInformation();
    }
}

代码和UML图(EA)工程文件,最后会整理打包上传.

UML类图

这里写图片描述

结构

  • Component(Employee):组合对象的抽象.
  • Leaf(ShopSaler):组合对象节点.
  • Composite(ShopKeeper):含有组合对象的节点对象.

优点

  • 定义了包含基本对象和组合对象的类层次结构.
  • 简化客户代码.
  • 使得更容易增加新类型的组件.
  • 使你的设计变得更加一般化.

缺点

  • 使设计变得更加抽象,对象的业务规则如果很复杂,则实现组合模式具有很大挑战性,而且不是所有的方法都与叶子对象子类都有关联.
  • 需要考虑”透明方式”和”安全方式”.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值