读《大话设计模式》---外观模式(Facade)

外观模式:

为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得一个子系统更加容易使用

 

外观模式结构图

 

外观模式的一般实现方法:

  1. #include <iostream>
  2. using namespace std;
  3. class SubSystemOne
  4. {
  5. public:
  6.     void MethodOne()
  7.     {
  8.         cout << " 子系统方法一" << endl;
  9.     }
  10. };
  11. class SubSystemTwo
  12. {
  13. public:
  14.     void MethodTwo()
  15.     {
  16.         cout << " 子系统方法二" << endl;
  17.     }
  18. };
  19. class SubSystemThree
  20. {
  21. public:
  22.     void MethodThree()
  23.     {
  24.         cout << " 子系统方法三" << endl;
  25.     }
  26. };
  27. class SubSystemFour
  28. {
  29. public:
  30.     void MethodFour()
  31.     {
  32.         cout << " 子系统方法四" << endl;
  33.     }
  34. };
  35. //外观类,它需要了解所有子系统的方法和属性,进行组合,以备外界使用。
  36. class Facade
  37. {
  38. private:
  39.     SubSystemOne * one;
  40.     SubSystemTwo * two;
  41.     SubSystemThree * three;
  42.     SubSystemFour * four;
  43. public:
  44.     Facade()
  45.     {
  46.         one = new SubSystemOne();
  47.         two = new SubSystemTwo();
  48.         three = new SubSystemThree();
  49.         four = new SubSystemFour();
  50.     }
  51.     ~Facade()
  52.     {
  53.         delete one;
  54.         delete two;
  55.         delete three;
  56.         delete four;
  57.     }
  58.     void MethodA()
  59.     {
  60.         cout << "/n方法组A() ---- " << endl;
  61.         one->MethodOne();
  62.         two->MethodTwo();
  63.         four->MethodFour();
  64.     }
  65.     
  66.     void MethodB()
  67.     {
  68.         cout << "/n方法组B() ---- " << endl;
  69.         two->MethodTwo();
  70.         three->MethodThree();
  71.     }
  72. };
  73. //由于Facade的存在,客户端根本不需要知道子系统的存在
  74. int main()
  75. {
  76.     Facade * facade = new Facade();
  77.     facade->MethodA();
  78.     facade->MethodB();
  79.     delete facade;
  80.     return 0;
  81. }

Facade模式的具体实现

  1. //投资基金代码
  2. #include <iostream>
  3. using namespace std;
  4. //股票1
  5. class Stock1
  6. {
  7.     //卖股票
  8. public
  9.     void Sell()
  10.     {
  11.         cout << " 股票1卖出" << endl;
  12.     }
  13.     
  14.     //买股票
  15.     void Buy()
  16.     {
  17.         cout << " 股票1买入" << endl;
  18.     }
  19. };
  20. //股票2
  21. class Stock2
  22. {
  23.     //卖股票
  24. public:
  25.     void Sell()
  26.     {
  27.         cout << " 股票2卖出" << endl;
  28.     }
  29.     
  30.     //买股票
  31.     void Buy()
  32.     {
  33.         cout << " 股票2买入" << endl;
  34.     }
  35. };
  36. //股票3
  37. class Stock3
  38. {
  39.     //卖股票
  40. public:
  41.     void Sell()
  42.     {
  43.         cout << " 股票3卖出" << endl;
  44.     }
  45.     
  46.     //买股票
  47.     void Buy()
  48.     {
  49.         cout << " 股票3买入" << endl;
  50.     }
  51. };
  52. //国债1
  53. class NationalDebt1
  54. {
  55.     //卖国债
  56. public:
  57.     void Sell()
  58.     {
  59.         cout << " 国债1卖出" << endl;
  60.     }
  61.     
  62.     //买国债
  63.     void Buy()
  64.     {
  65.         cout << " 国债1买入" << endl;
  66.     }
  67. };
  68. //房地产1
  69. class Realty1
  70. {
  71.     //卖房地产
  72. public:
  73.     void Sell()
  74.     {
  75.         cout << " 房产1卖出" << endl;
  76.     }
  77.     
  78.     //买房地产
  79.     void Buy()
  80.     {
  81.         cout << " 房产1买入" << endl;
  82.     }
  83. };
  84. class Fund
  85. {
  86. private:
  87.     Stock1 * gu1;
  88.     Stock2 * gu2;
  89.     Stock3 * gu3;
  90.     NationalDebt1 * nd1;
  91.     Realty1 * rt1;
  92.     
  93. public:
  94.     Fund()
  95.     {
  96.         gu1 = new Stock1();
  97.         gu2 = new Stock2();
  98.         gu3 = new Stock3();
  99.         nd1 = new NationalDebt1();
  100.         rt1 = new Realty1();
  101.     }
  102.     ~Fund()
  103.     {
  104.         delete gu1;
  105.         delete gu2;
  106.         delete gu3;
  107.         delete nd1;
  108.         delete rt1;
  109.     }
  110.     void BuyFund()
  111.     {
  112.         gu1->Buy();
  113.         gu2->Buy();
  114.         gu3->Buy();
  115.         nd1->Buy();
  116.         rt1->Buy();
  117.     }
  118.     void SellFund()
  119.     {
  120.         gu1->Sell();
  121.         gu2->Sell();
  122.         gu3->Sell();
  123.         nd1->Sell();
  124.         rt1->Sell();
  125.     }
  126. };
  127. int main()
  128. {
  129.     Fund * fund = new Fund();
  130.     
  131.     fund->BuyFund();
  132.     fund->SellFund();
  133.     
  134.     delete fund;
  135.     return 0;
  136. }

何时使用外观模式:

1.在设计阶段初期,要有意识的将不同的两个层分离,比如经典的三层架构,就需要考虑在数据访问层和业务逻辑层、业务逻辑层和表示层的层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口,使得耦合大大降低。

2.在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂。增加外观Facade可以提供一个简单的接口,减少它们之间的依赖

3.在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值