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