设计模式观后(c++还原之二十二 访问者模式)

//访问者模式
class IVisitor;
//抽象员工类
class Employee {
public:
    const int MALE = 0;//男性
    const int FEMALE = 1;//女
private:
    string m_strname;
    string m_nsalary;
    int m_nsex;
public:
    virtual string GetName() {
        return m_strname;
    }
    virtual void SetName(string name) {
        m_strname = name;
    }
    virtual string GetSalary() {
        return m_nsalary;
    }
    virtual void SetSalary(string salary) {
        m_nsalary = salary;
    }
    virtual int GetSex() {
        return m_nsex;
    }
    virtual void SetSex(int sex) {
        m_nsex = sex;
    }
    //允许一个访问者访问
    virtual void Accept(IVisitor* p_visitor){}
};
//普通员工
class CommonEmployee : public Employee {
private:
    string m_strjob;
public:
    //员工只要工作
    string GetJob() {
        return m_strjob;
    }
    void SetJob(string job) {
        m_strjob = job;
    }
    void Accept(IVisitor* p_visitor) {
        p_visitor->visit(this);
    }
};
class Manager : public Employee {
private:
    string m_strperformance;
public:
    //业绩
    string GetPerformance() {
        return m_strperformance;
    }
    void SetPerformance(string performance) {
        m_strperformance = performance;
    }
    void Accept(IVisitor* p_visitor) {
        p_visitor->visit(this);
    }
};
//访问者接口
class IVisitor {
public:
    //访问普通员工
    virtual void visit(CommonEmployee* common) = 0;
    //访问部门经理
    virtual void visit(Manager* mana) = 0;
};
class Visitor: public IVisitor {
private:
    //基本信息
    string GetBasicInfo(Employee* emp) {
        string info("姓名:");
        info += emp->GetName() + "\t";
        info += "性别:" + (emp->GetSex() == 1 ? "女":"男");
        info += "\t" + "薪水:";
        info += emp->GetSalary() + "\t";
        return info;
    }
    //部门经理信息
    string GetManagerInfo(Manager* emp) {
        string info(GetBasicInfo(emp));
        info += "业绩:" + emp->GetPerformance();
        return info;
    }
    //普通员工信息
    string GetCommonEmployeeInfo(CommonEmployee* emp) {
        string info(GetBasicInfo(emp));
        info += "工作:" + emp->GetJob();
        return info;
    }
public:
    void visit(CommonEmployee* common) {
        
    }
    void visit(Manager* mana) {
    }
};

class Client {
public:
    static void main() {
        list<Employee*> p = MockEmployee();
        for (list<Employee*>::iterator i = p.begin();
             i != p.end(); i++) {
            (*i)->Accept(new Visitor);
        }
    }
    static std::list<Employee*> MockEmployee() {
        list<Employee*> emp;
        emp.clear();
        
        CommonEmployee* zhangsan = new CommonEmployee;
        zhangsan->SetJob("编程");
        zhangsan->SetName("张三");
        zhangsan->SetSalary("1000");
        zhangsan->SetSex(1);
        emp.push_back(zhangsan);
        
        Manager* wangwu = new Manager;
        wangwu->SetName("王五");
        wangwu->SetPerformance("监督、拍马屁");
        wangwu->SetSalary("10000");
        wangwu->SetSex(1);
        emp.push_back(wangwu);
        
        return emp;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值