當(dāng)前位置:
首頁(yè) >
C++设计模式-外观模式
發(fā)布時(shí)間:2025/3/15
20
豆豆
生活随笔
收集整理的這篇文章主要介紹了
C++设计模式-外观模式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
?
?
基本概念
代碼與實(shí)例
?
基本概念
外觀模式(Facade):為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,此模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。
增加外觀Facade可以提供一個(gè)簡(jiǎn)單的接口,減少它們之間的依賴;
為新系統(tǒng)開(kāi)發(fā)一個(gè)外觀Facade類,來(lái)提供設(shè)計(jì)粗糙或高度復(fù)雜的遺留代碼的比較清晰簡(jiǎn)單的接口,讓新系統(tǒng)與Facade對(duì)象交互,Facade與遺留代碼交互所有復(fù)雜的工作。
?
剛開(kāi)始學(xué)面向?qū)ο蟪绦蛟O(shè)計(jì)的時(shí)候,基本上都會(huì)默認(rèn)采用這種方法,因?yàn)樗昝赖奶岈F(xiàn)了依賴倒轉(zhuǎn)原則和迪米特法則的思想。
?
UML圖如下:
?
代碼與實(shí)例
程序運(yùn)行截圖如下:
源碼如下:
Head.h
#ifndef HEAD_H #define HEAD_Hclass SubSystemOne; class SubSystemTwo; class SubSystemThree; class SubSystemFour;class Facade{public:Facade();~Facade();void methodA();void methodB();void methodC();private:SubSystemOne *one;SubSystemTwo *two;SubSystemThree *three;SubSystemFour *four; };class SubSystemOne{public:void methodOne(); };class SubSystemTwo{public:void methodTwo(); };class SubSystemThree{public:void methodThree(); };class SubSystemFour{public:void methodFour(); };#endif //HEAD_HHead.cpp
#include "Head.h" #include <iostream> #include <string> using namespace std;Facade::Facade() {one = new SubSystemOne;two = new SubSystemTwo;three = new SubSystemThree;four = new SubSystemFour; }Facade::~Facade() {delete one;delete two;delete three;delete four; }void Facade::methodA() {cout << "方法組A" << endl;one->methodOne();two->methodTwo();three->methodThree(); }void Facade::methodB() {cout << "方法組B" << endl;three->methodThree();four->methodFour();two->methodTwo(); }void Facade::methodC() {cout << "方法組C" << endl;one->methodOne();three->methodThree(); }void SubSystemOne::methodOne() {cout << "SubSystemOne::methodOne()" << endl; }void SubSystemTwo::methodTwo() {cout << "SubSystemTwo::methodTwo()" << endl; }void SubSystemThree::methodThree() {cout << "SubSystemThree::methodThree()" << endl; }void SubSystemFour::methodFour() {cout << "SubSystemFour::methodFour()" << endl; }main.cpp
#include "Head.h" #include <iostream> #include <string> using namespace std;int main(int *argc, int *argv[]){Facade *facada = new Facade;facada->methodA();cout << "---------------華麗分割線---------------"<< endl;facada->methodC();cout << "---------------華麗分割線---------------"<< endl;facada->methodB();delete facada;getchar();return 0; }?
總結(jié)
以上是生活随笔為你收集整理的C++设计模式-外观模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Qt实践| HTTP知识点-接入某图片验
- 下一篇: Qt文档阅读笔记-官方2D Painti