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();
    }
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章