外观模式(facade)c++版本

大话设计模式中的外观模式c++版本

/*
 * facade.cpp
 *
 *  Created on: Jul 25, 2017
 *      Author: clh01s@163.com
 *      外观模式
 *      为子系统中的一组接口提供一个一致的界面,
 *      此模式定义了一个高层接口,这个接口使得这
 *      一个子系统更加容易使用.
 */

#include <iostream>
using namespace std;

//股票类
class Stock1
{
public:
    void sell()
    {
        cout<<"股票1卖出!!!"<<endl;
    }

    void  buy()
    {
        cout<<"股票1买入!!!"<<endl;
    }
};

class Stock2
{
public:
    void sell()
    {
        cout<<"股票2卖出!!!"<<endl;
    }

    void  buy()
    {
        cout<<"股票2买入!!!"<<endl;
    }
};

class Stock3
{
public:
    void sell()
    {
        cout<<"股票3卖出!!!"<<endl;
    }

    void  buy()
    {
        cout<<"股票3买入!!!"<<endl;
    }
};

class Stock4
{
public:
    void sell()
    {
        cout<<"股票4卖出!!!"<<endl;
    }

    void  buy()
    {
        cout<<"股票4买入!!!"<<endl;
    }
};

class Stock5
{
public:
    void sell()
    {
        cout<<"股票5卖出!!!"<<endl;
    }

    void  buy()
    {
        cout<<"股票5买入!!!"<<endl;
    }
};

class Stock6
{
public:
    void sell()
    {
        cout<<"股票6卖出!!!"<<endl;
    }

    void  buy()
    {
        cout<<"股票6买入!!!"<<endl;
    }
};

//统筹操作类(类似基金)
class Fund
{
public:
    //在构造函数初始化股票类指针
    Fund()
    {
        _gu1 = new Stock1();
        _gu2 = new Stock2();
        _gu3 = new Stock3();
        _gu4 = new Stock4();
        _gu5 = new Stock5();
        _gu6 = new Stock6();
    }

    void buy()
    {
        _gu1->buy();
        _gu2->buy();
        _gu3->buy();
        _gu4->buy();
        _gu5->buy();
        _gu6->buy();
    }

    void sell()
    {
        _gu1->sell();
        _gu2->sell();
        _gu3->sell();
        _gu4->sell();
        _gu5->sell();
        _gu6->sell();
    }
private:
    //各个股票类的指针变量用于统一操作股票
    Stock1 *_gu1 = NULL;
    Stock2 *_gu2 = NULL;
    Stock3 *_gu3 = NULL;
    Stock4 *_gu4 = NULL;
    Stock5 *_gu5 = NULL;
    Stock6 *_gu6 = NULL;

};

int main()
{
    //用户可以对fund函数一无所知,直接调用相关函数即可,所有的操作都有fund的函数代理完成
    Fund fd;
    fd.buy();
    fd.sell();
    return 0;
}

外观模式优点(摘抄与《设计模式》):
1.它对客户屏蔽子系统组件,因而减少了客户处理的对象的数目并使得子系统使用起来更加方便
2.它实现了系统与客户的松耦合关系,而子系统内部的功能往往是紧耦合的.松耦合关系使得子系统组件的变化不会影响到它的客户.
3.如果应用需要,它并不限制他们使用子系统类.因此你可以在系统易用性和通用性之间加以选择
转载请标注源地址:http://blog.csdn.net/clh01s/article/details/76068231

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值