设计模式之- 外观模式(Facade Pattern)
生活随笔
收集整理的這篇文章主要介紹了
设计模式之- 外观模式(Facade Pattern)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
外觀模式
外觀模式(Facade Pattern):外部與一個(gè)子系統(tǒng)的通信必須通過(guò)一個(gè)統(tǒng)一的外觀對(duì)象進(jìn)行,為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,外觀模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。外觀模式又稱為門(mén)面模式,它是一種對(duì)象結(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; }類圖:
外觀模式感覺(jué)最簡(jiǎn)單了,相當(dāng)于把幾個(gè)獨(dú)立的接口寫(xiě)了一個(gè)統(tǒng)一的包裝類進(jìn)行了合并,并向外提供統(tǒng)一的調(diào)用接口,代碼一看便知!
轉(zhuǎn)載于:https://www.cnblogs.com/J1ac/p/9738379.html
總結(jié)
以上是生活随笔為你收集整理的设计模式之- 外观模式(Facade Pattern)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 手机红外发射器使用方法?
- 下一篇: 设计模式(一)Chain Of Resp