标签:
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> #include <vector> using namespace std; //抽象电话类 class BasePhone { public: virtual void Call(int num) = 0; virtual void Answer() = 0; }; //移动电话 class MobilePhone : public BasePhone { private: vector<BasePhone*> m_phoneVec; string m_Name; public: MobilePhone(string name) : m_Name(name) { } //添加通讯录,或者说铺电话线更恰当 void Add(BasePhone* phone) { m_phoneVec.push_back(phone); } //根据号码打电话 virtual void Call(int num) override { cout << m_Name << "用手机打电话, "; m_phoneVec[num]->Answer(); } //接电话 virtual void Answer() override { cout << m_Name << "用手机接电话" << endl; } }; class TelePhone : public BasePhone { private: vector<BasePhone*> m_phoneVec; string m_Name; public: TelePhone(string name) : m_Name(name) { } //添加通讯录,或者说铺电话线更恰当 void Add(BasePhone* phone) { m_phoneVec.push_back(phone); } //根据号码打电话 virtual void Call(int num) override { cout << m_Name << "用座机打电话, "; m_phoneVec[num]->Answer(); //这里直接根据基类来处理了,如果有其他处理,耦合性更大 } //接电话 virtual void Answer() override { cout << m_Name << "用座机接电话" << endl; } }; int _tmain(int argc, _TCHAR* argv[]) { MobilePhone* phoneA = new MobilePhone("小明"); TelePhone* phoneB = new TelePhone("小刚"); MobilePhone* phoneC = new MobilePhone("小蔡"); //为每个对象和每个对象之间都建立联系 phoneA->Add(phoneA); phoneA->Add(phoneB); phoneA->Add(phoneC); phoneB->Add(phoneA); phoneB->Add(phoneB); phoneB->Add(phoneC); phoneC->Add(phoneA); phoneC->Add(phoneB); phoneC->Add(phoneC); phoneA->Call(1); phoneB->Call(2); phoneC->Call(0); system("pause"); return 0; }结果:
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> #include <vector> using namespace std; class BasePhone; //中介者基类 class BaseMediator { public: virtual void Call(int num) = 0; virtual void Add(BasePhone* phone) = 0; }; //抽象电话类 class BasePhone { protected: BaseMediator* m_Mediator; public: //现在我只需要增加一个中介者就可以了 void SetMediator(BaseMediator* mediator) { m_Mediator = mediator; } //通过中介者向其他电话发送信息 virtual void Call(int num) { m_Mediator->Call(num); } virtual void Answer() = 0; }; //电子电话中继器 class ElectricMediator : public BaseMediator { private: vector<BasePhone*> phoneVec; public: //登记电话 void Add(BasePhone* phone) override { phoneVec.push_back(phone); } //拨号 virtual void Call(int num) override { phoneVec[num]->Answer(); } }; //移动电话 class MobilePhone : public BasePhone { private: string m_Name; public: MobilePhone(string name) : m_Name(name) { } //根据号码打电话 virtual void Call(int num) override { cout << m_Name << "用手机打电话, "; BasePhone::Call(num); } //接电话 virtual void Answer() override { cout << m_Name << "用手机接电话" << endl; } }; class TelePhone : public BasePhone { private: string m_Name; public: TelePhone(string name) : m_Name(name) { } //根据号码打电话 virtual void Call(int num) override { cout << m_Name << "用座机打电话, "; BasePhone::Call(num); } //接电话 virtual void Answer() override { cout << m_Name << "用座机接电话" << endl; } }; int _tmain(int argc, _TCHAR* argv[]) { MobilePhone* phoneA = new MobilePhone("小明"); TelePhone* phoneB = new TelePhone("小刚"); MobilePhone* phoneC = new MobilePhone("小蔡"); BaseMediator* mediator = new ElectricMediator(); //向中介者中注册电话 mediator->Add(phoneA); mediator->Add(phoneB); mediator->Add(phoneC); //电话只保存中介者的引用就可以了 phoneA->SetMediator(mediator); phoneB->SetMediator(mediator); phoneC->SetMediator(mediator); phoneA->Call(1); phoneB->Call(2); phoneC->Call(0); system("pause"); return 0; }结果:
标签:
原文地址:http://blog.csdn.net/puppet_master/article/details/51353551