有一个这样的设计:
#include<string>
using std::string;
class IEmail{
public:
virtual void setSender(const string & sender) = 0;
virtual void setReceiver(const string & receiver) = 0;
virtual void setContent(const string & content) = 0;
protected:
string _sender;
string _receiver;
string _content;
};
class Email:public IEmail{
virtual void setSender(const string & sender);
virtual void setReceiver(const string & receiver);
virtual void setContent(const string & content);
};
void Email::setSender(const string &sender){}
void Email::setReceiver(const string &receiver){}
void Email::setContent(const string &content){}
后来,有了新的需求:
1、支持新协议(POP3、SMTP、IMAP4),因此需要对content进行序列化与反序列化。
2、支持新内容类型(HTML、XML)。
#include<string>
using std::string;
class IContent{
public:
virtual const string & getAsString() = 0;
protected:
string _content;
};
class IEmail{
public:
virtual void setSender(const string & sender) = 0;
virtual void setReceiver(const string & receiver) = 0;
virtual void setContent(const IContent & content) = 0;
protected:
string _sender;
string _receiver;
IContent *_content;
};
class HtmlContent:public IContent{
public:
HtmlContent(const string &content);
virtual const string & getAsString();
};
const string & HtmlContent::getAsString(){
return _content;
}
HtmlContent::HtmlContent(const string &content){
_content = content;
}
class XmlContent:public IContent{
public:
XmlContent(const string &content);
virtual const string & getAsString();
};
const string & XmlContent::getAsString(){
return _content;
}
XmlContent::XmlContent(const string &content){
_content = content;
}
class Email:public IEmail{
public:
virtual void setSender(const string & sender);
virtual void setReceiver(const string & receiver);
virtual void setContent(const IContent & content);
};
void Email::setSender(const string &sender){}
void Email::setReceiver(const string &receiver){}
void Email::setContent(const IContent &content){
_content = (IContent*)&content;
}
呃....总感觉哪里不对。
网友评论