• 第十二章 牛市股票还会亏钱 --- 外观模式

    第十二章 牛市股票还会亏钱 --- 外观模式

    1. <?php
    2. //子系统1
    3. class SubSystemOne
    4. {
    5. public function methodOne()
    6. {
    7. echo "子系统方法1\n";
    8. }
    9. }
    10. //子系统2
    11. class SubSystemTwo
    12. {
    13. public function methodTwo()
    14. {
    15. echo "子系统方法2\n";
    16. }
    17. }
    18. //子系统3
    19. class SubSystemThree
    20. {
    21. public function methodThree()
    22. {
    23. echo "子系统方法3\n";
    24. }
    25. }
    26. //子系统4
    27. class SubSystemFourth
    28. {
    29. public function methodFourth()
    30. {
    31. echo "子系统方法4\n";
    32. }
    33. }
    34. // 外观方法
    35. class Facade
    36. {
    37. private $systemOne;
    38. private $systemTwo;
    39. private $systemThree;
    40. private $systemFour;
    41. function __construct()
    42. {
    43. $this->systemOne = new SubSystemOne();
    44. $this->systemTwo = new SubSystemTwo();
    45. $this->systemThree = new SubSystemThree();
    46. $this->systemFour = new SubSystemFourth();
    47. }
    48. public function methodA()
    49. {
    50. echo "方法A() ---\n";
    51. $this->systemOne->methodOne();
    52. $this->systemThree->methodThree();
    53. }
    54. public function methodB()
    55. {
    56. echo "方法B() ---\n";
    57. $this->systemTwo->methodTwo();
    58. $this->systemFour->methodFourth();
    59. }
    60. }
    61. //客户端代码
    62. $facade = new Facade();
    63. $facade->methodA();
    64. $facade->methodB();

    总结:

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

    首先,在设计初期阶段,应该要有意识的将不同的两个层分离,层与层之间建立外观Facade;其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,增加外观Facade可以提供一个简单的接口,减少它们之间的依赖;另外在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作

    上一章:第十一章 无熟人难办事 --- 迪米特法则

    下一章:第十三章 好菜每回味不同 --- 建造者模式