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

本文深入探讨了外观模式(Facade Pattern),一种简化系统接口的设计模式。通过一个C++代码示例,展示了如何将多个复杂子系统接口封装成一个统一的高级接口,使系统更易于使用。

外观模式

外观模式(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;
}

类图:

../_images/Facade.jpg

外观模式感觉最简单了,相当于把几个独立的接口写了一个统一的包装类进行了合并,并向外提供统一的调用接口,代码一看便知!

转载于:https://www.cnblogs.com/J1ac/p/9738379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值