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:
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.
Class Diagram:
The Facade pattern mainly includes the following three roles:
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.
- // 投影仪接口
- interface Projector {
- void on();
- void off();
- void focus();
- void wideScreenMode();
- }
-
- // 投影仪实现类
- class SimpleProjector implements Projector {
- @Override
- public void on() {
- System.out.println("Projector is on");
- }
-
- @Override
- public void off() {
- System.out.println("Projector is off");
- }
-
- @Override
- public void focus() {
- System.out.println("Adjusting projector focus");
- }
-
- @Override
- public void wideScreenMode() {
- System.out.println("Switching to wide screen mode");
- }
- }
-
- // DVD播放器接口
- interface DVDPlayer {
- void on();
- void off();
- void play();
- void stop();
- void pause();
- void eject();
- void setTwoChannelAudio();
- void setSurroundAudio();
- }
-
- // DVD播放器实现类
- class SimpleDVDPlayer implements DVDPlayer {
- @Override
- public void on() {
- System.out.println("DVD Player is on");
- }
-
- @Override
- public void off() {
- System.out.println("DVD Player is off");
- }
-
- @Override
- public void play() {
- System.out.println("DVD Player is playing");
- }
-
- @Override
- public void stop() {
- System.out.println("DVD Player stopped");
- }
-
- @Override
- public void pause() {
- System.out.println("DVD Player paused");
- }
-
- @Override
- public void eject() {
- System.out.println("DVD ejected");
- }
-
- @Override
- public void setTwoChannelAudio() {
- System.out.println("Stereo mode is on");
- }
-
- @Override
- public void setSurroundAudio() {
- System.out.println("Surround sound mode is on");
- }
- }
-
- // 音响接口
- interface Amplifier {
- void on();
- void off();
- void setStereoSound();
- void setSurroundSound();
- void setVolume(int volume);
- }
-
- // 音响实现类
- class StereoAmplifier implements Amplifier {
- @Override
- public void on() {
- System.out.println("Amplifier is on");
- }
-
- @Override
- public void off() {
- System.out.println("Amplifier is off");
- }
-
- @Override
- public void setStereoSound() {
- System.out.println("Stereo mode is on");
- }
-
- @Override
- public void setSurroundSound() {
- System.out.println("Surround sound mode is on");
- }
-
- @Override
- public void setVolume(int volume) {
- System.out.println("Setting volume to " + volume);
- }
- }
-
- // 家庭影院Facade类
- class HomeTheaterFacade {
- private Projector projector;
- private DVDPlayer dvdPlayer;
- private Amplifier amplifier;
-
- public HomeTheaterFacade(Projector projector, DVDPlayer dvdPlayer, Amplifier amplifier) {
- this.projector = projector;
- this.dvdPlayer = dvdPlayer;
- this.amplifier = amplifier;
- }
-
- // 提供一个简单的方法来观看电影
- public void watchMovie(String movie) {
- System.out.println("Get ready to watch a movie...");
-
- projector.on();
- projector.wideScreenMode();
-
- amplifier.on();
- amplifier.setSurroundSound();
- amplifier.setVolume(5);
-
- dvdPlayer.on();
- dvdPlayer.play(movie);
-
- System.out.println("Enjoy the movie...");
- }
-
- // 提供一个简单的方法来结束观看
- public void endMovie() {
- System.out.println("Shutting movie theater down...");
-
- projector.off();
- amplifier.off();
- dvdPlayer.stop();
- dvdPlayer.eject();
- dvdPlayer.off();
- }
- }
-
- class Client{
- public static void main(String[] args) {
- Projector projector = new SimpleProjector();
- DVDPlayer dvdPlayer = new SimpleDVDPlayer();
- Amplifier amplifier = new StereoAmplifier();
- HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(projector,dvdPlayer,amplifier);
- homeTheaterFacade.watchMovie("肖申克的救赎");
- homeTheaterFacade.endMovie();
- }
- }
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.
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.