標籤:design 對象 使用 awr 設計 pattern 面板模式 類圖 pac
面板模式面板模式(Facade Pattern):外部與一個子系統的通訊必須通過一個統一的外觀對象進行,為子系統中的一組介面提供一個一致的介面,面板模式定義了一個高層介面,這個介面使得這一子系統更加容易使用。面板模式又稱為門面模式,它是一種對象結構型模式。 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;}
類圖:
面板模式感覺最簡單了,相當於把幾個獨立的介面寫了一個統一的封裝類進行了合并,並向外提供統一的調用介面,代碼一看便知!
設計模式之- 面板模式(Facade Pattern)