结构型模式-组合(composite)

组合

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

实例

main.cc:

#include 
#include "shop_saler.h"
#include "shop_keeper.h"
#include 
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 
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 
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 
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 
using namespace std;

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


//shop_keeper.cc
#include "shop_keeper.h"
#include 
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::iterator v = list_employee_.begin();v != list_employee_.end();v++){
        (*v)->ShowInformation();
    }
}

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

UML类图

结构型模式-组合(composite)_第1张图片

结构

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

优点

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

缺点

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

你可能感兴趣的:(设计模式,设计模式,组合模式)