Appearance Mode (Facade Mode)
Definition
Here's a reference from Chapter 7 of Headfirst Design Patterns
Appearance patterns provide a unified interface for accessing a group of interfaces in a subsystem. Appearance defines a high-level interface that makes subsystems easier to use.
Advantages and disadvantages
The following references are from Reference 1
The Facade pattern is a typical application of the Dimiter's Law and has the following main advantages.
- Decreases the coupling between the subsystem and the client so that changes in the subsystem do not affect the client class calling it.
- Subsystem components are shielded from customers, reducing the number of objects processed by customers, and making subsystems easier to use.
- It reduces compilation dependency in large software systems and simplifies the migration process between different platforms, since compiling a subsystem will not affect other subsystems or the appearance objects.
The main drawbacks of the Facade pattern are as follows.
- Customers are not well restricted from using subsystem classes, and unknown risks can easily arise.
- Adding a new subsystem may require modifying the appearance class or client source code, which violates the Open and Close Principle.
Use scenarios
The following references are from Reference 1
Appearance patterns are often considered in the following situations.
- When building a hierarchical system, defining entry points for each layer in the subsystem using the appearance pattern simplifies the dependencies between subsystems.
- When there are many subsystems in a complex system, appearance patterns can provide a simple interface for system design for external access.
- When there is a strong connection between clients and multiple subsystems, the introduction of appearance patterns can separate them, thereby improving the independence and portability of subsystems.
Example
Here's an example code from Chapter 7 of the head first design pattern
//Appearance Role public class HomeTheaterFacade { //Subsystem Role Amplifier amp; //Subsystem Role Tuner tuner; //Subsystem Role StreamingPlayer player; //Subsystem Role CdPlayer cd; //Subsystem Role Projector projector; //Subsystem Role TheaterLights lights; //Subsystem Role Screen screen; //Subsystem Role PopcornPopper popper; public HomeTheaterFacade(Amplifier amp, Tuner tuner, StreamingPlayer player, Projector projector, Screen screen, TheaterLights lights, PopcornPopper popper) { this.amp = amp; this.tuner = tuner; this.player = player; this.projector = projector; this.screen = screen; this.lights = lights; this.popper = popper; } public void watchMovie(String movie) { System.out.println("Get ready to watch a movie..."); popper.on(); popper.pop(); lights.dim(10); screen.down(); projector.on(); projector.wideScreenMode(); amp.on(); amp.setStreamingPlayer(player); amp.setSurroundSound(); amp.setVolume(5); player.on(); player.play(movie); } public void endMovie() { System.out.println("Shutting movie theater down..."); popper.off(); lights.on(); screen.up(); projector.off(); amp.off(); player.stop(); player.off(); } public void listenToRadio(double frequency) { System.out.println("Tuning in the airwaves..."); tuner.on(); tuner.setFrequency(frequency); amp.on(); amp.setVolume(5); amp.setTuner(tuner); } public void endRadio() { System.out.println("Shutting down the tuner..."); tuner.off(); amp.off(); } }
Client
public class HomeTheaterTestDrive { public static void main(String[] args) { Amplifier amp = new Amplifier("Amplifier"); Tuner tuner = new Tuner("AM/FM Tuner", amp); StreamingPlayer player = new StreamingPlayer("Streaming Player", amp); CdPlayer cd = new CdPlayer("CD Player", amp); Projector projector = new Projector("Projector", player); TheaterLights lights = new TheaterLights("Theater Ceiling Lights"); Screen screen = new Screen("Theater Screen"); PopcornPopper popper = new PopcornPopper("Popcorn Popper"); HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, player, projector, screen, lights, popper); homeTheater.watchMovie("Raiders of the Lost Ark"); homeTheater.endMovie(); } }
Reference resources
1Detailed Appearance Mode (Facade Mode)
2 Headfirst Design Mode