状态模式(State Pattern)

状态模式(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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flyfish1986/article/details/52370406
文章标签: 状态模式
个人分类: 设计模式
所属专栏: 设计模式
上一篇读书《用户体验要素 以用户为中心的产品设计》
下一篇MFC 解压7z文件
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭