c++设计模式四,接口模式

来源:互联网 发布:ajax跨域请求不是json 编辑:程序博客网 时间:2024/06/05 02:43

接口模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。简单说,就是将复杂的逻辑封装起来,对外公开简单的接口,由客户程序调用。

看代码:

iletterprocess.h 基类

#ifndef ILETTERPROCESS_H#define ILETTERPROCESS_H#include <iostream>using std::string;class ILetterProcess{public:    ILetterProcess(void){}    virtual ~ILetterProcess(void){}    virtual void WriteContext(string context) = 0;    virtual void FillEnvelope(string address) = 0;    virtual void LetterIntoEnvelope() = 0;    virtual void SendLetter() = 0;};#endif // ILETTERPROCESS_H

letterpolice.h 邮差

#ifndef LETTERPOLICE_H#define LETTERPOLICE_H#include "ILetterProcess.h"class CLetterPolice{public:    CLetterPolice(void);    ~CLetterPolice(void);    void CheckLetter(ILetterProcess *pLetterProcess);};#endif // LETTERPOLICE_H
letterpolice.cpp文件

#include "letterpolice.h"CLetterPolice::CLetterPolice(void){}CLetterPolice::~CLetterPolice(void){}void CLetterPolice::CheckLetter( ILetterProcess *pLetterProcess ){    //检查信件,此处省略一万字。    return;}
letterprocessimpl.h 文件

#ifndef LETTERPROCESSIMPL_H#define LETTERPROCESSIMPL_H#include "iletterprocess.h"class CLetterProcessImpl :    public ILetterProcess{public:    CLetterProcessImpl(void);    ~CLetterProcessImpl(void);    void WriteContext(string context);    void FillEnvelope(string address);    void LetterIntoEnvelope();    void SendLetter();};#endif // LETTERPROCESSIMPL_H
letterprocessimpl.cpp文件

#include "letterprocessimpl.h"#include <iostream>using std::string;using std::cout;using std::endl;CLetterProcessImpl::CLetterProcessImpl(void){}CLetterProcessImpl::~CLetterProcessImpl(void){}void CLetterProcessImpl::WriteContext(string context){    cout << "填写信的内容... ..." << endl;}void CLetterProcessImpl::FillEnvelope(string address){    cout << "填写收件人地址及姓名... ..." << endl;}void CLetterProcessImpl::LetterIntoEnvelope(){    cout << "把信放到信封中..." << endl;}void CLetterProcessImpl::SendLetter(){    cout << "邮递信件..." << endl;}
modenpostoffice.h文件

#ifndef MODENPOSTOFFICE_H#define MODENPOSTOFFICE_H#include "ILetterProcess.h"#include "LetterProcessImpl.h"#include "LetterPolice.h"#include <iostream>using std::string;class CModenPostOffice{public:    CModenPostOffice(void);    ~CModenPostOffice(void);    void SendLetter(string context, string address);private:    ILetterProcess *m_pLetterProcess;    CLetterPolice *m_pLetterPolice;};#endif // MODENPOSTOFFICE_H
modenpostoffice.cpp文件

#include "modenpostoffice.h"CModenPostOffice::CModenPostOffice(void){    this->m_pLetterProcess = new CLetterProcessImpl();    this->m_pLetterPolice = new CLetterPolice();}CModenPostOffice::~CModenPostOffice(void){    delete m_pLetterProcess;    delete m_pLetterPolice;}void CModenPostOffice::SendLetter( string context, string address ){    //帮忙写信    m_pLetterProcess->WriteContext(context);    //写好信封    m_pLetterProcess->FillEnvelope(address);    //警察要检查信件了    m_pLetterPolice->CheckLetter(m_pLetterProcess);    //把信放到信封中    m_pLetterProcess->LetterIntoEnvelope();    //邮递信件    m_pLetterProcess->SendLetter();}

main.cpp文件

#include "ILetterProcess.h"#include "LetterProcessImpl.h"#include "ModenPostOffice.h"#include<iostream>using std::string;using std::cout;using std::endl;void DoItByPostOffice(){    CModenPostOffice modenPostOffice;    string context = "Hello, It's me, do you know who I am? I'm your old lover. I'd like to ... ...";    string address = "Happy Road No. 666, Beijing City, China";    modenPostOffice.SendLetter(context, address);}void DoItYourself(){    ILetterProcess *pLetterProcess = new CLetterProcessImpl();    pLetterProcess->WriteContext("Hello, It's me, do you know who I am? I'm your old lover. I'd like to ... ...");    pLetterProcess->FillEnvelope("Happy Road No. 666, Beijing City, China");    pLetterProcess->LetterIntoEnvelope();    pLetterProcess->SendLetter();    delete pLetterProcess;}int main(int argc, char* argv[]){    //现在的调用方式。对于客户来说确实简单多了。    //如需要增加逻辑,例如让警察来检查邮件。可以在邮局里完成这项工作。    DoItByPostOffice();    //原来的调用方式。    DoItYourself();    return 0;}
运行结果;