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

来源:互联网 发布:全网举报 淘宝 编辑:程序博客网 时间:2024/06/05 04:50
        

外观模式:

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

外观模式结构图

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

  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. void MethodB()
  66. {
  67. cout << "/n方法组B() ---- " << endl;
  68. two->MethodTwo();
  69. three->MethodThree();
  70. }
  71. };
  72. //由于Facade的存在,客户端根本不需要知道子系统的存在
  73. int main()
  74. {
  75. Facade * facade = new Facade();
  76. facade->MethodA();
  77. facade->MethodB();
  78. delete facade;
  79. return 0;
  80. }

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

何时使用外观模式:

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

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

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

原创粉丝点击