zoukankan      html  css  js  c++  java
  • 责任链模式

    #include <iostream>
    
    using namespace std;
    
    class Handler
    {
    private:
        int mMsg;
        Handler *mNextHandler;
    public:
        Handler(int msg) :mMsg(msg), mNextHandler(NULL) {}
        ~Handler() {}
    public:
        virtual void handleMessage(int message) = 0;
    
        int getMessage()
        {
            return mMsg;
        }
    
        Handler *getNextHandler()
        {
            return mNextHandler;
        }
    
        void setNextHandler(Handler *handler)
        {
            mNextHandler = handler;
        }
    };
    
    class MouseHandler : public Handler
    {
    public:
        MouseHandler(int msg) :Handler(msg) {}
        ~MouseHandler() {}
    public:
        virtual void handleMessage(int message)
        {
            cout << "MouseHandler handled the message : " << message << endl;
        }
    };
    
    class CatHandler : public Handler
    {
    public:
        CatHandler(int msg) :Handler(msg) {}
        ~CatHandler() {}
    public:
        virtual void handleMessage(int message)
        {
            cout << "CatHandler handled the message : " << message << endl;
        }
    };
    
    class DogHandler : public Handler
    {
    public:
        DogHandler(int msg) :Handler(msg) {}
        ~DogHandler() {}
    public:
        virtual void handleMessage(int message)
        {
            cout << "DogHandler handled the message : " << message << endl;
        }
    };
    
    void looper(int message, Handler *handler)
    {
        Handler *temp = handler;
    
        while (true)
        {
            if (temp->getMessage() == message)
            {
                temp->handleMessage(message);
                break;
            }
            else if (temp->getMessage() == 0)
            {
                temp->handleMessage(message);
                break;
            }
            else
            {
                if (temp->getNextHandler() != NULL)
                {
                    temp = temp->getNextHandler();
                }
                else
                {
                    cout << "no handler!" << endl;
                    break;
                }
            }
        }
    }
    
    int main(int argc, char const *argv[])
    {
        Handler *mouseHandler = new MouseHandler(1);
        Handler *catHandler = new CatHandler(2);
        Handler *dogHandler = new DogHandler(0);
    
        mouseHandler->setNextHandler(catHandler);
        catHandler->setNextHandler(dogHandler);
    
        looper(2, mouseHandler);
        looper(-1, mouseHandler);
    
        delete dogHandler;
        dogHandler = NULL;
    
        delete catHandler;
        catHandler = NULL;
    
        delete mouseHandler;
        mouseHandler = NULL;
    
        return 0;
    }
  • 相关阅读:
    session的工作原理
    jsp与servlet
    java对象的克隆以及深拷贝与浅拷贝
    vi和vim编辑器
    远程登录Linux系统(使用xshell),远程上传加载文件(使用Xftp)
    Linux目录结构(目录结构详解是重点)
    虚拟机CentOS克隆
    虚拟机CentOS创建/使用快照
    VMware Tools安装
    VM配置Centos(第十三步分区设置)
  • 原文地址:https://www.cnblogs.com/chen-cai/p/9773274.html
Copyright © 2011-2022 走看看