Technology Sharing

Design pattern of appearance mode (Facade)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

The Facade design pattern, also known as the appearance pattern, is a structural design pattern. It is mainly used to provide a unified high-level interface for a group of interfaces in a subsystem, making the subsystem easier to use. The following is a detailed introduction to the Facade design pattern:

I. Definition

The Facade pattern provides an external interface for multiple complex subsystems, making these subsystems easier to access. This pattern has a unified external interface, and external applications do not need to care about the details of the subsystem, which greatly reduces the complexity of the application and improves maintainability.

2. Motivation and Purpose

  • Reduce complexity: By encapsulating the complexity of the subsystem in the Facade class, the client can call the subsystem through a simple interface without having to have an in-depth understanding of the specific implementation of the subsystem.
  • Improve usability: The Facade pattern provides a simple interface that makes it easier for clients to use the subsystem.
  • Loose coupling: By introducing the Facade class, the coupling between the client and the subsystem is reduced, so that changes within the subsystem will not affect the client.

3. Structure and Class Diagram

Class Diagram:

The Facade pattern mainly includes the following three roles:

  • Facade:The client can call the methods of this role. The facade class knows the functions and responsibilities of the relevant (one or more) subsystems. Under normal circumstances, this role will delegate all requests sent from the client to the corresponding subsystem.
  • Subsystem: There can be one or more subsystems at the same time. Each subsystem is not a separate class, but a collection of classes. Each subsystem can be called directly by the client or by the facade role. The subsystem is unaware of the existence of the facade. For the subsystem, the facade is just another client.
  • Client:Users call the subsystem functions through the appearance class.

4. Advantages and Disadvantages

advantage
  1. Reduce system complexity: By providing a simple interface for the subsystem, it makes it easier for clients to understand and use the system.
  2. Improve system flexibility: When changes occur within the subsystem, only the Facade class needs to be modified without modifying the client code.
  3. The law of Demeter: By introducing the Facade class, the dependency between the client and the subsystem is reduced.
shortcoming
  1. Adding subsystems or extending subsystem behavior can easily introduce risks: Because all requests need to be forwarded through the Facade class, if the Facade class is not designed properly, new risks may be introduced.
  2. Not in compliance with the opening and closing principle: In some cases, if the system needs to be expanded, the code of the Facade class may need to be modified, which violates the open-closed principle (that is, open for extension and closed for modification).

5. Application Scenarios

  • When the system needs to provide a simple interface to the outside world, the Facade pattern can be used.
  • The Facade pattern can be used when a system needs to hide the complexity of a subsystem.
  • When you need to build a multi-layer system structure, you can use the Facade object as the entrance to each layer to simplify hierarchical calls.

6. Implementation Example

In a home theater system, the Facade pattern can be used to simplify user operations. For example, users only need to use one remote control (Facade class) to control devices (subsystems) such as projectors, audio systems, and DVD players, without having to operate these devices separately.

  1. // 投影仪接口
  2. interface Projector {
  3. void on();
  4. void off();
  5. void focus();
  6. void wideScreenMode();
  7. }
  8. // 投影仪实现类
  9. class SimpleProjector implements Projector {
  10. @Override
  11. public void on() {
  12. System.out.println("Projector is on");
  13. }
  14. @Override
  15. public void off() {
  16. System.out.println("Projector is off");
  17. }
  18. @Override
  19. public void focus() {
  20. System.out.println("Adjusting projector focus");
  21. }
  22. @Override
  23. public void wideScreenMode() {
  24. System.out.println("Switching to wide screen mode");
  25. }
  26. }
  27. // DVD播放器接口
  28. interface DVDPlayer {
  29. void on();
  30. void off();
  31. void play();
  32. void stop();
  33. void pause();
  34. void eject();
  35. void setTwoChannelAudio();
  36. void setSurroundAudio();
  37. }
  38. // DVD播放器实现类
  39. class SimpleDVDPlayer implements DVDPlayer {
  40. @Override
  41. public void on() {
  42. System.out.println("DVD Player is on");
  43. }
  44. @Override
  45. public void off() {
  46. System.out.println("DVD Player is off");
  47. }
  48. @Override
  49. public void play() {
  50. System.out.println("DVD Player is playing");
  51. }
  52. @Override
  53. public void stop() {
  54. System.out.println("DVD Player stopped");
  55. }
  56. @Override
  57. public void pause() {
  58. System.out.println("DVD Player paused");
  59. }
  60. @Override
  61. public void eject() {
  62. System.out.println("DVD ejected");
  63. }
  64. @Override
  65. public void setTwoChannelAudio() {
  66. System.out.println("Stereo mode is on");
  67. }
  68. @Override
  69. public void setSurroundAudio() {
  70. System.out.println("Surround sound mode is on");
  71. }
  72. }
  73. // 音响接口
  74. interface Amplifier {
  75. void on();
  76. void off();
  77. void setStereoSound();
  78. void setSurroundSound();
  79. void setVolume(int volume);
  80. }
  81. // 音响实现类
  82. class StereoAmplifier implements Amplifier {
  83. @Override
  84. public void on() {
  85. System.out.println("Amplifier is on");
  86. }
  87. @Override
  88. public void off() {
  89. System.out.println("Amplifier is off");
  90. }
  91. @Override
  92. public void setStereoSound() {
  93. System.out.println("Stereo mode is on");
  94. }
  95. @Override
  96. public void setSurroundSound() {
  97. System.out.println("Surround sound mode is on");
  98. }
  99. @Override
  100. public void setVolume(int volume) {
  101. System.out.println("Setting volume to " + volume);
  102. }
  103. }
  104. // 家庭影院Facade类
  105. class HomeTheaterFacade {
  106. private Projector projector;
  107. private DVDPlayer dvdPlayer;
  108. private Amplifier amplifier;
  109. public HomeTheaterFacade(Projector projector, DVDPlayer dvdPlayer, Amplifier amplifier) {
  110. this.projector = projector;
  111. this.dvdPlayer = dvdPlayer;
  112. this.amplifier = amplifier;
  113. }
  114. // 提供一个简单的方法来观看电影
  115. public void watchMovie(String movie) {
  116. System.out.println("Get ready to watch a movie...");
  117. projector.on();
  118. projector.wideScreenMode();
  119. amplifier.on();
  120. amplifier.setSurroundSound();
  121. amplifier.setVolume(5);
  122. dvdPlayer.on();
  123. dvdPlayer.play(movie);
  124. System.out.println("Enjoy the movie...");
  125. }
  126. // 提供一个简单的方法来结束观看
  127. public void endMovie() {
  128. System.out.println("Shutting movie theater down...");
  129. projector.off();
  130. amplifier.off();
  131. dvdPlayer.stop();
  132. dvdPlayer.eject();
  133. dvdPlayer.off();
  134. }
  135. }
  136. class Client{
  137. public static void main(String[] args) {
  138. Projector projector = new SimpleProjector();
  139. DVDPlayer dvdPlayer = new SimpleDVDPlayer();
  140. Amplifier amplifier = new StereoAmplifier();
  141. HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(projector,dvdPlayer,amplifier);
  142. homeTheaterFacade.watchMovie("肖申克的救赎");
  143. homeTheaterFacade.endMovie();
  144. }
  145. }

In this example, we can see the facade pattern, which is particularly suitable for using multiple components, assembling fixed processes, and reducing the complexity of client calls.

VII. Conclusion

The Facade design pattern is a very practical design pattern that reduces the complexity of the system and improves the usability and flexibility of the system by providing a unified interface for the subsystem. However, when using this pattern, you also need to pay attention to its possible shortcomings and risks.

If this mode is useful to you, please like and collect it.