[GoF设计模式]Prototype模式和Mediator模式的C++实现

【Prototype模式】

用原型实例指定创建对象的种类,被且通过拷贝这些原型对象创建新的对象。拷贝构造函数应该注意深拷贝和浅拷贝的问题。深拷贝主要是指有指针或者复合对象的情况,而浅拷贝对此只是拷贝了指针,而与其共享同一个副本。编译器提供的默认的拷贝构造函数是按位拷贝的值(Value)类型的拷贝方式。Prototype(下例中的HousePrototype)声明一个克隆自身的接口,而ConcretePrototype(下例中的ApartmentConcretePrototype)则实现一个克隆自身的操作。主调函数则利用一个原型克隆自身从而创建一个新的对象。对客户隐藏了具体的产品类。

【图解】

以住房类(HousePrototype)为例子,其中的CloneMyself函数接口是直接调用拷贝构造函数来实现其自身的克隆,所以在子类(ApartmentConcretePrototype)中的拷贝构造函数的实现是关键所在,要注意深拷贝和浅拷贝的问题,经可能避免内存泄漏。

[GoF设计模式]Prototype模式和Mediator模式的C++实现_第1张图片

 

【代码】

 /******************************************************************* * * DESCRIPTION:House Prototype Class [Abstract Class] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef HOUSEPROTOTYPE_H_ #define HOUSEPROTOTYPE_H_ /** include files **/ #include <iostream> using namespace std; class HousePrototype { public: virtual ~HousePrototype(){} /// <summary> /// Just call the copy constructor /// </summary> /// <returns></returns> virtual HousePrototype* CloneMySelf()=0; protected:/*Abstract Class*/ HousePrototype(){} }; #endif  

/******************************************************************* * * DESCRIPTION:Apartment Class [Concrete Class] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef APARTMENTCONCRETEPROTOTYPE_H_ #define APARTMENTCONCRETEPROTOTYPE_H_ /** include files **/ #include "HousePrototype.h" typedef string Aircondition; class ApartmentConcretePrototype:public HousePrototype { public: ApartmentConcretePrototype(){} virtual ~ApartmentConcretePrototype(){} /// <summary> /// Copy Construtor-Be carefule about the deep copy and shallow copy /// [注意深拷贝以及浅拷贝的问题]:示例代码不设计深拷贝(主要是指针以及复合对象的情况) /// 编译器提供的默认的拷贝构造函数是一种“按位拷贝”的方式。 /// </summary> /// <param name="cp"></param> /// <returns></returns> ApartmentConcretePrototype(const ApartmentConcretePrototype& cp) { cout<<"/nApartmentConcretePrototype: My Copy Constructor being called......"<<endl; } /*Implement the interface */ virtual HousePrototype * CloneMySelf() { return new ApartmentConcretePrototype(*this); /*Call the Copy Constructor*/ } }; #endif  

/******************************************************************* * * DESCRIPTION: Prototype Pattern[原型模式] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ /** include files **/ #include <iostream> #include "HousePrototype.h" #include "ApartmentConcretePrototype.h" using namespace std; int main (int argc, char *argv[]) { HousePrototype* housePrototype=new ApartmentConcretePrototype(); HousePrototype* cHouse=housePrototype->CloneMySelf(); return(0); }  

 

【输出】

 2

 

【Mediator模式】

中介者模式是利用一个中介对象来封装一些列的对象交互。中介者使得各对象不需要显示地相互引用,从而达到松耦合的目的,而且可以使得它们之间的交互可以独立地改变。主要是用于对象较多时,交互关系错综复杂,可以使用其来达到解耦的目的。中介者(Mediator)提供统一的接口用于交互双方(Colleague)之间的通信,具体的中介者(Concrete Mediator)则通过协调各同事以实现协作的行为,因此要知道并引用各同事(Colleague)。由于中介者的行为与要使用的数据与业务关系紧密,抽象的中介者提供一个方便很多对象使用的接口是不现实的,只起到标识接口的作用,往往可以省略。中介者模式很容易应用到系统中,也很容易误用,但系统出现"多对多"交互复杂的对象群时,要予以考虑,一组对象以定义良好但是复杂的方式进行通信,产生了混乱的依赖关系,也导致对象难以复用时,要考虑。[应注意与observer模式的搭配使用来反映发布者和订阅者之间的关系,如果发布者和订阅者很多,关系错综的情况下]

【图解】

以房屋买卖中介为例子,买卖双方都是具体的Concrete Colleague,而房屋的中介是具体的Concrete Mediator。卖方向关联的中介提出出售请求,中介通知关联的买房,过程也可以相反。

 

[GoF设计模式]Prototype模式和Mediator模式的C++实现_第2张图片

【代码】

/******************************************************************* * * DESCRIPTION: BusinessMediator Class [Abstract Class 作生意的中介] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef BUSINESSMEDIATOR_H_ #define BUSINESSMEDIATOR_H_ /** include files **/ #include "BusinessManColleague.h" class BusinessManColleague;/*Predeclaration*/ /** * BusinessMediator Class [ Abstract Class ] * * @author roy nee (2009-10-5) */ class BusinessMediator { public: virtual ~BusinessMediator(){} virtual void DoBusinessFromBuyertoSaler()=0; virtual void DoBusinessFromSalertoBuyer()=0; protected: BusinessMediator(){} }; #endif

/******************************************************************* * * DESCRIPTION:HouseBusinessMediator [Concrete Class] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef HOUSEBUSINESSMEDIATOR_H_ #define HOUSEBUSINESSMEDIATOR_H_ /** include files **/ #include "BusinessMediator.h" class HouseBusinessMediator:public BusinessMediator { public: /// <summary> /// Constructor for the HouseBusinessMediator /// </summary> /// <returns></returns> HouseBusinessMediator(){} /// <summary> /// Set the Business Two Parts /// </summary> /// <param name="saler"></param> /// <param name="buyer"></param> /// <returns></returns> virtual void SetSalerAndBuyerColleague(BusinessManColleague *saler,BusinessManColleague *buyer) { this->_saler=saler; this->_buyer=buyer; return; } virtual ~HouseBusinessMediator(){} /// <summary> /// Now saler give the House to the buyer /// </summary> /// <returns></returns> virtual void DoBusinessFromBuyertoSaler() { cout<<"/nHouse Mediator: Buyer, here someone wanna sale his house......"<<endl; this->_saler->DoBusinessAnswer(); return; } /// <summary> /// Now buyer give the Money to the saler /// </summary> /// <returns></returns> virtual void DoBusinessFromSalertoBuyer() { cout<<"/nHouse Mediator: Saler, here someone wanna buy your house......"<<endl; this->_buyer->DoBusinessAnswer(); return; } private: BusinessManColleague *_saler;//House Saler BusinessManColleague *_buyer;//House Buyer }; #endif

/******************************************************************* * * DESCRIPTION: BusinessMan Colleague Class [Abstract Class] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef BUSINESSMANCOLLEAGUE_H_ #define BUSINESSMANCOLLEAGUE_H_ /** include files **/ #include "BusinessMediator.h" class BusinessMediator; /*Predeclaration*/ class BusinessManColleague { public: virtual ~BusinessManColleague(){} /// <summary> /// Tell Business Mediator that I wanna do Business[提出交易] /// </summary> /// <returns></returns> virtual void DoBusinessAsk()=0; /// <summary> /// Tell Business Mediator that I agree this Business[接受交易] /// </summary> /// <returns></returns> virtual void DoBusinessAnswer()=0; protected: BusinessManColleague(BusinessMediator *mediator) { this->_mediator=mediator; } BusinessMediator *_mediator; }; #endif

/******************************************************************* * * DESCRIPTION:House Buyer Colleague Class [买房者] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef HOUSEBUYERCOLLEAGUE_H_ #define HOUSEBUYERCOLLEAGUE_H_ /** include files **/ #include <iostream> #include "BusinessManColleague.h" using namespace std; class HouseBuyerColleague:public BusinessManColleague { public: /// <summary> /// Constructor Function /// </summary> /// <param name="mediator"></param> /// <returns></returns> HouseBuyerColleague(BusinessMediator *mediator):BusinessManColleague(mediator) { } virtual ~HouseBuyerColleague(){} /// <summary> /// Tell Business Mediator that I do Business /// </summary> /// <returns></returns> virtual void DoBusinessAsk() { this->_mediator->DoBusinessFromBuyertoSaler(); cout<<"House Buyer(Ask): Now I give the money to the Saler......"<<endl; return; } /// <summary> /// Tell Business Mediator that I agree this business /// </summary> /// <returns></returns> virtual void DoBusinessAnswer() { cout<<"House Buyer(Answer): Now I give the money to the Saler......"<<endl; return; } }; #endif

/******************************************************************* * * DESCRIPTION:House Saler Colleague Class [卖房者] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ #ifndef HOUSESALERCOLLEAGUE_H_ #define HOUSESALERCOLLEAGUE_H_ /** include files **/ #include <iostream> #include "BusinessManColleague.h" using namespace std; /** * * * @author roy nee (2009-10-5) */ class HouseSalerColleague:public BusinessManColleague { public: /// <summary> /// Constructor Function /// </summary> /// <param name="mediator"></param> /// <returns></returns> HouseSalerColleague(BusinessMediator *mediator):BusinessManColleague(mediator) { } virtual ~HouseSalerColleague(){} /// <summary> /// Tell Business Mediator that I do Business /// </summary> /// <returns></returns> virtual void DoBusinessAsk() { this->_mediator->DoBusinessFromSalertoBuyer(); cout<<"House Saler(Ask): Now I give the house to the Buyer......"<<endl; return; } /// <summary> /// Tell Business Mediator that I agree this business /// </summary> /// <returns></returns> virtual void DoBusinessAnswer() { cout<<"House Saler(Answer): Now I give the house to the Buyer......"<<endl; return; } }; #endif

/******************************************************************* * * DESCRIPTION:Mediator Pattern [中介者模式] * * AUTHOR:Neesky * * DATE:2009-10-5 * *******************************************************************/ /** include files **/ #include <iostream> using namespace std; #include "HouseBusinessMediator.h" #include "BusinessManColleague.h" #include "BusinessMediator.h" #include "HouseBuyerColleague.h" #include "HouseSalerColleague.h" int main (int argc, char *argv[]) { /*There is a Mediator*/ HouseBusinessMediator *mediator=new HouseBusinessMediator(); /*There are buyer and saler*/ HouseSalerColleague *saler=new HouseSalerColleague(mediator); HouseBuyerColleague *buyer=new HouseBuyerColleague(mediator); /*Now Set the two part*/ mediator->SetSalerAndBuyerColleague(saler,buyer); /*买房向中介提出买*/ buyer->DoBusinessAsk(); /*卖方向中介提出卖*/ saler->DoBusinessAsk(); return(0); }

 

【输出】

[GoF设计模式]Prototype模式和Mediator模式的C++实现_第3张图片

你可能感兴趣的:([GoF设计模式]Prototype模式和Mediator模式的C++实现)