[设计模式]外观模式
2021/6/15 10:51:14
本文主要是介绍[设计模式]外观模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
外观模式:为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。
代码如下:
#include <iostream> using namespace std; class Television { public: void on() { cout << "Tv on" << endl; } void off() { cout << "Tv off" << endl; } }; class Light { public: void on() { cout << "Light on" << endl; } void off() { cout << "Light off" << endl; } }; class Audio { public: void on() { cout << "Audio on" << endl; } void off() { cout << "Audio off" << endl; } }; class Microphone { public: void on() { cout << "Microphone on" << endl; } void off() { cout << "Microphone off" << endl; } }; class Dvd { public: void on() { cout << "Dvd on" << endl; } void off() { cout << "Dvd off" << endl; } }; class KTVMode { public: KTVMode() { pTv = new Television; pLight = new Light; pAudio = new Audio; pMicrophone = new Microphone; pDvd = new Dvd; } void onKtv() { pTv->on(); pLight->on(); pAudio->on(); pMicrophone->on(); pDvd->on(); } void offKtv() { pTv->off(); pLight->off(); pAudio->off(); pMicrophone->off(); pDvd->off(); } private: Television *pTv; Light *pLight; Audio *pAudio; Microphone *pMicrophone; Dvd *pDvd; ~KTVMode() { delete pTv; delete pLight; delete pAudio; delete pMicrophone; delete pDvd; } }; void test01() { KTVMode *ktv = new KTVMode; ktv->onKtv(); } int main() { test01(); return 0; }
测试结果:
这篇关于[设计模式]外观模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27消息中间件底层原理资料详解
- 2024-11-27RocketMQ底层原理资料详解:新手入门教程
- 2024-11-27MQ底层原理资料详解:新手入门教程
- 2024-11-27MQ项目开发资料入门教程
- 2024-11-27RocketMQ源码资料详解:新手入门教程
- 2024-11-27本地多文件上传简易教程
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器