Read big talk design patterns --- Facade)

Source: Internet
Author: User

Appearance mode:

Provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes it easier for a subsystem to use.

 

Visual mode structure

 

The general implementation of the appearance mode:

  1. # Include <iostream>
  2. Using namespace STD;
  3. Class subsystemone
  4. {
  5. Public:
  6. Void methodone ()
  7. {
  8. Cout <"Subsystem Method 1" <Endl;
  9. }
  10. };
  11. Class subsystemtwo
  12. {
  13. Public:
  14. Void methodtwo ()
  15. {
  16. Cout <"Subsystem Method 2" <Endl;
  17. }
  18. };
  19. Class subsystemthree
  20. {
  21. Public:
  22. Void methodthree ()
  23. {
  24. Cout <"Subsystem Method 3" <Endl;
  25. }
  26. };
  27. Class subsystemfour
  28. {
  29. Public:
  30. Void methodfour ()
  31. {
  32. Cout <"Subsystem Method 4" <Endl;
  33. }
  34. };
  35. // Appearance class. It needs to understand the methods and attributes of all subsystems and combine them for external use.
  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 method group A () ----" <Endl;
  61. One-> methodone ();
  62. Two-> methodtwo ();
  63. Four-> methodfour ();
  64. }
  65. Void methodb ()
  66. {
  67. Cout <"/n method group B () ----" <Endl;
  68. Two-> methodtwo ();
  69. Three-> methodthree ();
  70. }
  71. };
  72. // Because of the existence of facade, the client does not need to know the existence of the subsystem.
  73. Int main ()
  74. {
  75. Facade * facade = new facade ();
  76. Facade-> methoda ();
  77. Facade-> methodb ();
  78. Delete facade;
  79. Return 0;
  80. }

Specific implementation of the facade Mode

  1. // Investment fund code
  2. # Include <iostream>
  3. Using namespace STD;
  4. // Stock 1
  5. Class stock1
  6. {
  7. // Sell stocks
  8. Public:
  9. Void Merge ()
  10. {
  11. Cout <"stock 1 sold" <Endl;
  12. }
  13. // Buy stock
  14. Void buy ()
  15. {
  16. Cout <"stock 1 buy" <Endl;
  17. }
  18. };
  19. // Stock 2
  20. Class stock2
  21. {
  22. // Sell stocks
  23. Public:
  24. Void Merge ()
  25. {
  26. Cout <"stock 2 sold" <Endl;
  27. }
  28. // Buy stock
  29. Void buy ()
  30. {
  31. Cout <"stock 2 buy" <Endl;
  32. }
  33. };
  34. // Stock 3
  35. Class stock3
  36. {
  37. // Sell stocks
  38. Public:
  39. Void Merge ()
  40. {
  41. Cout <"stock 3 sold" <Endl;
  42. }
  43. // Buy stock
  44. Void buy ()
  45. {
  46. Cout <"stock 3 buy" <Endl;
  47. }
  48. };
  49. // National debt 1
  50. Class nationaldebt1
  51. {
  52. // Sell government bonds
  53. Public:
  54. Void Merge ()
  55. {
  56. Cout <" 1 sold" <Endl;
  57. }
  58. // Buy government bonds
  59. Void buy ()
  60. {
  61. Cout <"National Debt 1 buy" <Endl;
  62. }
  63. };
  64. // Real Estate 1
  65. Class realty1
  66. {
  67. // Sell real estate
  68. Public:
  69. Void Merge ()
  70. {
  71. Cout <"property 1 sold" <Endl;
  72. }
  73. // Buy real estate
  74. Void buy ()
  75. {
  76. Cout <"buy 1 Property" <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-> callback ();
  115. Gu2-> callback ();
  116. Gu3-> callback ();
  117. Nd1-> encrypt ();
  118. Rt1-> Reset ();
  119. }
  120. };
  121. Int main ()
  122. {
  123. Fund * Fund = new fund ();
  124. Fund-> buyfund ();
  125. Fund-> sellfund ();
  126. Delete fund;
  127. Return 0;
  128. }

When to use the appearance mode:

1. at the early stage of the design stage, we must consciously separate two different layers, such as the classic three-layer architecture, you need to consider establishing an external facade between the data access layer and the business logic layer, the business logic layer, and the presentation layer Layer. In this way, you can provide a simple interface for complicated subsystems, this greatly reduces coupling.

2. In the development stage, subsystems often become more and more complex due to continuous reconstruction and evolution. By adding a facade, you can provide a simple interface to reduce the dependency between them.

3. when maintaining a legacy large system, it may be very difficult to maintain and expand. You can develop a facade class for the new system, to provide clear and simple interfaces for the design of rough or highly complex legacy code, so that the new system can interact with the facade object, and the facade and legacy code can interact with all the complicated work.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.