c++ 组合模式

#ifndef Staff_hpp
#define Staff_hpp

#define CC_SYNTHESIZE(varType, varName, funName)\
protected: varType varName;\
public: virtual varType get##funName(void) const { return varName; }\
public: virtual void set##funName(varType var){ varName = var; }

#include <stdio.h>
#include <set>
#include <iostream>
using namespace std;
class Staff {
public:
    Staff();
    Staff(string name,int age ,string job);
    virtual string getInfo();
    virtual void getMyStaff();
    virtual void getBossInfo();
    CC_SYNTHESIZE(string,highname,Highname);
    CC_SYNTHESIZE(string,name,Name);
    CC_SYNTHESIZE(int,age,Age);
    CC_SYNTHESIZE(string,job,Job);
protected:
    std::set<Staff*> mySet;
};

#endif /* Staff_hpp */

//
//  Staff.cpp
//  HelloWorld
//
//  Created by happytree on 16/3/7.
//  Copyright © 2016年 happytree. All rights reserved.
//

#include "Staff.hpp"

Staff::Staff() {

}

Staff::Staff(string name,int age ,string job) {
    this->name = name ;
    this->age = age;
    this->job = job;
}

string Staff::getInfo() {
    printf("name: %s, age : %d , job : %s\n" ,this->name.c_str(),this->age,this->job.c_str());
    return "";
}

void Staff::getMyStaff() {
    this->getInfo();
    set<Staff*>::iterator rit;
    for(rit=mySet.begin();rit!=mySet.end();rit++) {
        (*rit)->getMyStaff();
    }
}

void Staff::getBossInfo() {
    printf("name: %s, boss : %s \n" ,this->name.c_str(),this->highname.c_str());
}

#ifndef Normal_hpp
#define Normal_hpp

#include <stdio.h>
#include "Staff.hpp"
class Normal:public Staff {
public:
    Normal(std::string name,int age ,std::string job);
};
#endif /* Normal_hpp */

#include "Normal.hpp"

Normal::Normal(std::string name,int age ,std::string job) {
    Staff::Staff(name,age,job);
    this->setName(name);
    this->setAge(age);
    this->setJob(job);
}

#ifndef Vip_hpp
#define Vip_hpp

#include <stdio.h>
#include <set>
#include "Normal.hpp"
class Vip:public Staff {
public:
    Vip(std::string name,int age ,std::string job);
    void addStaff(Normal* staff);
    void addVip(Vip* vip);
    virtual void getMyStaff();
};
#endif /* Vip_hpp */

#include "Vip.hpp"
Vip::Vip(std::string name,int age ,std::string job) {
    Staff::Staff(name,age,job);
    this->setName(name);
    this->setAge(age);
    this->setJob(job);
    this->setHighname("");
}

void Vip::getMyStaff() {
    printf("--------%s手下begin-----------\n",this->getName().c_str());
    Staff::getMyStaff();
    printf("--------%s手下end-----------\n",this->getName().c_str());
}

void Vip::addStaff(Normal* staff) {
    mySet.insert(staff);
    staff->setHighname(this->getName());
}

void Vip::addVip(Vip* vip) {
    mySet.insert(vip);
    vip->setHighname(this->getName());
}

/**
 组合模式,将员工类抽象出来,再进行添加组合
 */
#include <iostream>
#include "Vip.hpp"
#include "Normal.hpp"


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, Coming!\n";
    Vip *viperA = new Vip("viperA",45,"项目经理1");
    Vip *viperB = new Vip("viperB",40,"小项目经理");
    Vip *viperC = new Vip("viperC",41,"项目经理2");

    Normal *normalA = new Normal("员工A",20,"秘书");
    Normal *normalB = new Normal("员工B",20,"业务工");
    Normal *normalC = new Normal("员工C",20,"搬运工");
    Normal *normalD = new Normal("员工D",20,"技术");
    Normal *normalF = new Normal("员工F",20,"技术");
    
    viperA->addStaff(normalA);
    viperA->addVip(viperB);
    viperB->addStaff(normalB);
    viperB->addStaff(normalC);
    viperC->addStaff(normalD);
    viperC->addStaff(normalF);
    
//    viperA->getMyStaff();
//    viperB->getMyStaff();
//    viperC->getMyStaff();
    
    set<Staff*> staffList;
    staffList.insert(viperA);
    staffList.insert(viperB);
    staffList.insert(viperC);
    staffList.insert(normalA);
    staffList.insert(normalB);
    staffList.insert(normalC);
    staffList.insert(normalD);
    staffList.insert(normalF);
    set<Staff *> ::iterator staff;
    for(staff = staffList.begin() ; staff != staffList.end();staff ++) {
        (*staff)->getBossInfo();
    }
    
}

### C++组合模式的实现与使用 #### 1. 组合模式简介 组合模式(Composite Pattern)允许客户端一致地处理单个对象和复合对象。通过这种方式,程序能够透明地操作树形结构中的各个节点,而无需关心这些节点是叶子还是容器[^3]。 #### 2. 基本接口定义 为了使组件之间具有统一的操作接口,在C++中通常会创建一个抽象基来声明公共的行为。这个基既可以被具体的叶节点继承也可以被容器型的子所扩展: ```cpp class Component { public: virtual ~Component() {} // 定义通用的方法签名 virtual void operation() const = 0; }; ``` #### 3. 叶子组件实现 对于不包含任何子部件的对象,则可以直接派生自`Component`并提供具体的功能实现: ```cpp class Leaf : public Component { public: void operation() const override { std::cout << "Leaf Operation\n"; } }; ``` #### 4. 容器组件实现 当某个实体可能拥有多个子项时, 就应该构建一个新的去管理这些成员变量,并重写父方法以便于调用内部各部分的服务: ```cpp #include <vector> using namespace std; class Composite : public Component { private: vector<Component*> children_; public: void add(Component* child) { children_.push_back(child); } void remove(Component* child) { auto it = find(children_.begin(), children_.end(), child); if(it != end(children_)) children_.erase(it); } void operation() const override { cout << "Branch Operation:\n"; for(auto& comp : children_) { comp->operation(); } } }; ``` #### 5. 使用示例 最后可以通过如下方式实例化上述来进行测试或者业务逻辑开发: ```cpp int main(){ Composite root; root.add(new Leaf()); Composite branch; branch.add(new Leaf()); branch.add(new Leaf()); root.add(&branch); root.operation(); return 0; } ``` 此代码片段展示了如何利用组合模式建立层次化的数据结构,并且可以在不影响整体架构的前提下轻松添加更多功能模块或改变现有行为。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值