• 中介者模式(C++)


    #include <iostream>
    #include <string>
    using namespace std;
    
    class absperson;
    class absmed
    {
    public:
        absmed(){}
        virtual ~absmed(){}
        virtual void send(string,absperson*)=0;
        virtual void set(absperson*,absperson*)=0;
    };
    
    class absperson
    {
    public:
        absperson(){}
        virtual ~absperson(){}
        virtual void send(string)=0;
        virtual void get(string)=0;
    protected:
        absmed *p_med;
    };
    
    class person_buy : public absperson
    {
    public:
        person_buy(absmed *p){p_med=p;}
        virtual ~person_buy(){}
        void send(string str)
        {
            p_med->send(str,this);
        }
    
        void get(string str)
        {
            cout<<"买主得到信息:"<<str<<endl;
        }
    };
    
    class person_sell : public absperson
    {
    public:
        person_sell(absmed *p){p_med=p;}
        virtual ~person_sell(){}
        void send(string str)
        {
            p_med->send(str,this);
        }
    
        void get(string str)
        {
            cout<<"卖主得到信息:"<<str<<endl;
        }
    };
    
    class med : public absmed
    {
    protected:
        absperson *person1;
        absperson *person2;
    public:
        med(){}
        virtual ~med(){}
        void send(string str,absperson* p)
        {
            if (p==person1)
                person2->get(str);
            else
                person1->get(str);
        }
    
        void set(absperson *p1, absperson *p2)
        {
            person1=p1;
            person2=p2;
        }
    };
    
    int main()
    {
        absmed *p_med=new med;
        absperson *p_buy=new person_buy(p_med);
        absperson *p_sell=new person_sell(p_med);
    
        p_med->set(p_buy,p_sell);
        p_buy->send("我想买电脑");
        p_sell->send("哥卖的不是电脑,是寂寞");
        p_buy->send("好吧,你赢了");
        
        delete p_med;
        delete p_buy;
        delete p_sell;
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    计算机执行程序代码的过程分析
    iOS 操作系统架构
    ios 概况了解
    android ApplicationContext Context Activity 内存的一些学习
    android 内存优化一
    android 学习
    ios 基础学习二
    ios 集合总结
    线程之间的通讯---SynchronizationContext
    ASP.NET Core 身份验证及鉴权
  • 原文地址:https://www.cnblogs.com/tiandsp/p/2567628.html
Copyright © 2020-2023  润新知