最初版本
#include <iostream>
class Message
{
public:virtual void Login(std::string username, std::string password) = 0;virtual void SendMessage(std::string messages) = 0;virtual void SendPicture(Image image) = 0;virtual void PalySound() = 0;virtual void DrawShape() = 0;virtual void WirteText() = 0;virtual void Connect() = 0;virtual ~Message() {};
};class PCMessageBase : public Message
{
public:virtual void PalySound(){}virtual void DrawShape(){}virtual void WirteText(){}virtual void Connect() {}
};class MobileMessageBase : public Message
{
public:virtual void PalySound(){}virtual void DrawShape(){}virtual void WirteText(){}virtual void Connect() {}
};
class PCMessagePerfect : public PCMessageBase
{
public:virtual void Login(std::string username, std::string password){PCMessageBase::PalySound();/*******/PCMessageBase::Connect();}virtual void SendMessage (std::string message){PCMessageBase::PalySound();/*******/PCMessageBase::WirteText();}virtual void SendPicture(Image image){PCMessageBase::PalySound();/*******/PCMessageBase::DrawShape();}
};
class PCMessageLite : public PCMessageBase
{
public:virtual void Login(std::string username, std::string password){PCMessageBase::Connect();}virtual void SendMessage (std::string message){PCMessageBase::WirteText();}virtual void SendPicture(Image image){PCMessageBase::DrawShape();}
};class MobileMessagePerfect : public MobileMessageBase
{
public:virtual void Login(std::string username, std::string password){MobileMessageBase::PalySound();/*******/MobileMessageBase::Connect();}virtual void SendMessage (std::string message){MobileMessageBase::PalySound();/*******/MobileMessageBase::WirteText();}virtual void SendPicture(Image image){MobileMessageBase::PalySound();/*******/MobileMessageBase::DrawShape();}
};
class MobileMessageList : public MobileMessageBase
{
public:virtual void Login(std::string username, std::string password){MobileMessageBase::Connect();}virtual void SendMessage (std::string message){MobileMessageBase::WirteText();}virtual void SendPicture(Image image){MobileMessageBase::DrawShape();}
};int main()
{Message * msg = new PCMessagePerfect();
}
最终版本
#include <iostream>
class Image
{};class MessageImp;class Message
{
protected:MessageImp* messageImp;
public:Message(MessageImp* messageImp) : messageImp(messageImp) {}virtual void Login(std::string username, std::string password) = 0;virtual void SendMessage(std::string messages) = 0;virtual void SendPicture(Image image) = 0;virtual ~Message() {};
};class MessageImp
{
public:virtual void PalySound() = 0;virtual void DrawShape() = 0;virtual void WirteText() = 0;virtual void Connect() = 0;virtual ~MessageImp() {};
};
class PCMessageImp : public MessageImp
{
public:virtual void PalySound(){}virtual void DrawShape(){}virtual void WirteText(){}virtual void Connect(){}
};
class MobileMessageImp : public MessageImp
{
public:virtual void PalySound(){}virtual void DrawShape(){}virtual void WirteText(){}virtual void Connect(){}
};
class MessagePerfect : public Message
{
public:MessagePerfect(MessageImp* messageImp):Message(messageImp){}virtual void Login(std::string username, std::string password){messageImp->PalySound();/*******/messageImp->Connect();}virtual void SendMessage(std::string message){messageImp->PalySound();/*******/messageImp->WirteText();}virtual void SendPicture(Image image){messageImp->PalySound();/*******/messageImp->DrawShape();}
};
class MessageLite :public Message
{
public:MessageLite(MessageImp* messageImp) :Message(messageImp){}virtual void Login(std::string username, std::string password){messageImp->Connect();}virtual void SendMessage(std::string message){messageImp->WirteText();}virtual void SendPicture(Image image){messageImp->DrawShape();}
};
int main()
{MessageImp* imp = new MobileMessageImp();Message* mge = new MessagePerfect(imp);
}