码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之- 外观模式(Facade Pattern)

时间:2018-10-03 00:50:31      阅读:157      评论:0      收藏:0      [点我收藏+]

标签: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)

标签:design   对象   使用   awr   设计   pattern   外观模式   类图   pac   

原文地址:https://www.cnblogs.com/J1ac/p/9738379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!