C++设计模式-外观模式
生活随笔
收集整理的這篇文章主要介紹了
C++设计模式-外观模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
基本概念
代碼與實例
?
基本概念
外觀模式(Facade):為子系統中的一組接口提供一個一致的界面,此模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
增加外觀Facade可以提供一個簡單的接口,減少它們之間的依賴;
為新系統開發一個外觀Facade類,來提供設計粗糙或高度復雜的遺留代碼的比較清晰簡單的接口,讓新系統與Facade對象交互,Facade與遺留代碼交互所有復雜的工作。
?
剛開始學面向對象程序設計的時候,基本上都會默認采用這種方法,因為它完美的提現了依賴倒轉原則和迪米特法則的思想。
?
UML圖如下:
?
代碼與實例
程序運行截圖如下:
源碼如下:
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; }?
總結
以上是生活随笔為你收集整理的C++设计模式-外观模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt实践| HTTP知识点-接入某图片验
- 下一篇: Qt文档阅读笔记-官方2D Painti