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_Hletterpolice.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_Hletterprocessimpl.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_Hmodenpostoffice.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;}运行结果;
阅读全文
0 0
- c++设计模式四,接口模式
- C语言的设计模式-接口隔离
- C语言设计模式与接口
- 设计模式(四)C语言版
- C#----设计模式接口 工厂模式的理解
- 设计模式(c++)笔记之四(Builder 模式)
- 设计模式---接口型模式
- 设计模式(四)---代理模式
- 设计模式四:代理模式
- 设计模式四 命令模式
- 设计模式<四> -命令模式
- 设计模式(四)-策略模式
- 设计模式四:原型模式
- 【设计模式 四】适配器模式
- 设计模式四(工厂模式)
- 设计模式六大原则(四)接口隔离原则
- 6大设计模式(四)----接口隔离原则
- 设计模式六大原则之四:接口隔离原则
- 【设计模式】控制反转(IoC)与依赖注入(DI)
- java中double运算的精度丢失问题
- VMware vcenter上传文件失败且vsphere client登陆失败。
- Android深度探索:HAL与驱动开发学习笔记--中断
- vpn
- c++设计模式四,接口模式
- 数据库的九大对象
- NB-IoT的发展是否一片光明?
- 第5周项目1- 建立顺序栈算法库
- 第五周项目一—建立顺序栈算法库
- JSP 标准标签库(JSTL)
- vue中使用v-bind:class的选项卡
- tomcat 报错 java.lang.OutOfMemoryError ( PermGen space)异常的解决方法
- 面向对象-多态