关闭

状态模式(State Pattern)

标签: 状态模式
133人阅读 评论(0) 收藏 举报
分类:

状态模式(State Pattern)

flyfish

#include "stdafx.h"
#include <iostream>


class role;
class employee
{
public:
    virtual void output(role *role_ptr) = 0;

};

class engineer : public employee
{

public:
    virtual void output(role *role_ptr)
    {
        std::cout << "engineer" << std::endl;
    }
};

class salesman : public employee
{
public:
    virtual void output(role *role_ptr)
    {
        std::cout << "salesman" << std::endl;
    }

};
class manager : public employee
{

public:
    virtual void output(role *role_ptr)
    {
        std::cout << "manager" << std::endl;
    }
};
class role
{
private:
    employee* employee_ptr_;
public:
    role(employee *employee_ptr) : employee_ptr_(employee_ptr){}

    void output()
    {

        if (employee_ptr_)
        {
            employee_ptr_->output(this);

        }
    }

    void change_role(employee *employee_ptr)
    {
        employee_ptr_ = employee_ptr;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{

    employee *a = new engineer();
    employee *b = new salesman();
    employee *c = new manager();

    role *r = new role(a);

    r->output();
    r->change_role(b);
    r->output();
    r->change_role(c);
    r->output();

    delete r;
    delete a;
    delete b;
    delete c;

    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:651547次
    • 积分:10122
    • 等级:
    • 排名:第1755名
    • 原创:364篇
    • 转载:0篇
    • 译文:2篇
    • 评论:102条
    博客专栏