设计模式之- 外观模式(Facade Pattern)
生活随笔
收集整理的這篇文章主要介紹了
设计模式之- 外观模式(Facade Pattern)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
外觀模式
外觀模式(Facade Pattern):外部與一個子系統(tǒng)的通信必須通過一個統(tǒng)一的外觀對象進行,為子系統(tǒng)中的一組接口提供一個一致的界面,外觀模式定義了一個高層接口,這個接口使得這一子系統(tǒng)更加容易使用。外觀模式又稱為門面模式,它是一種對象結(jié)構(gòu)型模式。 C++代碼: #include<iostream> using namespace std;class Shape { public:virtual void draw()=0; };class Rectangle : public Shape { public:void draw() {cout<<"Rectangle::draw()"<<endl;} };class Square : public Shape { public:void draw() {cout<<"Square ::draw()"<<endl;} };class Circle : public Shape { public:void draw() {cout<<"Circle ::draw()"<<endl;} };class ShapeMaker {Shape *circle;Shape *rectangle;Shape *square;public:ShapeMaker() {circle = new Circle();rectangle = new Rectangle();square = new Square();}void drawCircle(){circle->draw();}void drawRectangle(){rectangle->draw();}void drawSquare(){square->draw();} };class FacadePatternDemo { public: static void method(int argc,char**argv) {ShapeMaker *shapeMaker = new ShapeMaker();shapeMaker->drawCircle();shapeMaker->drawRectangle();shapeMaker->drawSquare(); } };int main(int argc,char**argv){FacadePatternDemo::method(argc,argv);return 0; }類圖:
外觀模式感覺最簡單了,相當于把幾個獨立的接口寫了一個統(tǒng)一的包裝類進行了合并,并向外提供統(tǒng)一的調(diào)用接口,代碼一看便知!
轉(zhuǎn)載于:https://www.cnblogs.com/J1ac/p/9738379.html
總結(jié)
以上是生活随笔為你收集整理的设计模式之- 外观模式(Facade Pattern)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机红外发射器使用方法?
- 下一篇: 有没有什么安卓软件能让手机当遥控器遥控电