[design mode from introduction to mastery] 09 - appearance mode

Note source: Shang Silicon Valley Java design pattern (illustration + framework source code analysis)

Appearance mode

1. Cinema Management Project

Set up a home theater:

DVD player, projector, automatic screen, surround sound and popcorn machine are required to complete the function of using home theater. The process is as follows:

  • Direct remote control: coordinate the switch of each equipment
  • Popcorn opener
  • Drop the screen
  • Turn on the projector
  • Turn on the stereo
  • Open DVD, select DVD
  • Go get the popcorn
  • Dim light
  • play
  • After viewing, turn off all kinds of equipment

Traditional way to solve Cinema Management

ClientTest{
    public static void main(String[] args){
        // 1. Create related objects
        // 2. Call a series of methods for each object created
        // 3. Call the play method of the dvdlayer object
    }
}

Analysis of traditional ways to solve Cinema Management Problems

  • 1) In the main method of ClientTest, creating the objects of each subsystem and directly calling the relevant methods of the subsystem (object) will cause confusion in the calling process and no clear process
  • 2) It is not conducive to maintaining the operation of the subsystem in ClientTest
  • 3) Solution: define a high-level interface to provide a consistent interface for a group of interfaces in the subsystem (for example, four methods ready, play, pause and end are provided in the high-level interface to access a group of interfaces in the subsystem
  • 4) In other words, a consistent interface (interface class) is defined to shield the details of the internal subsystem, so that the caller only needs to call the interface without caring about the internal details of the subsystem = = "appearance mode"

2. Basic introduction to appearance mode

Facade, also known as process mode

Appearance mode provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface to shield the details of the internal subsystem, so that the caller only needs to call this interface without paying attention to the internal details of the subsystem. This interface makes this subsystem easier to use

Schematic class diagram

Description of schematic class diagram (role of appearance mode)

  • 1) Facade: provides a unified calling interface for the caller. The facade knows which subsystems are responsible for processing requests, so as to proxy the caller's requests to appropriate subsystem objects
  • 2) Caller (Client): the caller of the appearance interface
  • 3) Subsystem collection: refers to a module or subsystem that handles the tasks assigned by the Facade object and is the actual provider of functions

3. Appearance mode to solve Cinema Management

  • 1) The appearance mode can be understood as converting a group of interfaces. Customers can achieve the purpose only by calling one interface instead of multiple interfaces, such as:
    • When installing software on a PC, there is often a one click installation option (omitting the selection of installation directory, installed components, etc.)
    • Restart function of mobile phone (combining shutdown and startup into one operation)
  • 2) Appearance mode is to solve the difficulties caused by multiple complex interfaces and simplify user operations

Use appearance mode to complete the home theater project

UML class diagram

Core code

[projector]

public class Projector {
    private static Projector projector = new Projector();

    public static Projector getInstance() {
        return projector;
    }

    public void on() {
        System.out.println("Turn on the projector...");
    }

    public void off() {
        System.out.println("Turn off the projector...");
    }

    public void focus() {
        System.out.println("Projector focus...");
    }

    public void zoom() {
        System.out.println("Projector magnification...");
    }
}

[DVD player]

public class DVDPlayer {
    private static DVDPlayer player = new DVDPlayer();

    public static DVDPlayer getInstance() {
        return player;
    }

    public void on() {
        System.out.println("open DVD player...");
    }

    public void off() {
        System.out.println("close DVD player...");
    }

    public void play() {
        System.out.println("play DVD player...");
    }

    public void pause() {
        System.out.println("suspend DVD player...");
    }

    public void setDvd(String dvd) {
        System.out.println("choose dvd: " + dvd + "...");
    }
}

[screen]

public class Screen {
    private static Screen screen = new Screen();

    public static Screen getInstance() {
        return screen;
    }

    public void up() {
        System.out.println("Raise the screen...");
    }

    public void down() {
        System.out.println("Pull down the screen...");
    }
}

[stereo]

public class Stereo {
    private static Stereo stereo = new Stereo();

    public static Stereo getInstance() {
        return stereo;
    }

    public void on() {
        System.out.println("Turn on stereo...");
    }

    public void off() {
        System.out.println("Turn off stereo...");
    }

    public void setVolume(Integer volume) {
        System.out.println("Stereo volume+" + volume + "...");
    }
}

[lighting]

public class TheaterLights {
    private static TheaterLights lights = new TheaterLights();

    public static TheaterLights getInstance() {
        return lights;
    }

    public void on() {
        System.out.println("Turn on the light...");
    }

    public void off() {
        System.out.println("Turn off the lights...");
    }

    public void dim() {
        System.out.println("Dim light...");
    }

    public void bright() {
        System.out.println("Turn on the light...");
    }
}

[popcorn machine]

public class Popcorn {
    private static Popcorn popcorn = new Popcorn();

    public static Popcorn getInstance() {
        return popcorn;
    }

    public void on() {
        System.out.println("Turn on the popcorn machine...");
    }

    public void off() {
        System.out.println("Turn off the popcorn machine...");
    }

    public void pop() {
        System.out.println("Take out the popcorn...");
    }
}

[home theater Facade]

public class HomeTheaterFacade {
    private Popcorn popcorn;
    private Screen screen;
    private Stereo stereo;
    private TheaterLights lights;
    private Projector projector;
    private DVDPlayer player;

    public HomeTheaterFacade() {
        this.popcorn = Popcorn.getInstance();
        this.screen = Screen.getInstance();
        this.stereo = Stereo.getInstance();
        this.lights = TheaterLights.getInstance();
        this.projector = Projector.getInstance();
        this.player = DVDPlayer.getInstance();
    }

    public void ready() {
        lights.on(); // Turn on the light
        popcorn.on(); // Popcorn opener
        screen.down(); // Drop the screen
        projector.on(); // Turn on the projector
        projector.focus();
        projector.zoom();
        stereo.on(); // Turn on the audio and set the volume
        stereo.setVolume(8);
        player.on(); // Open DVD, select DVD
        player.setDvd("Tantanik");
        popcorn.pop(); // Go get the popcorn and turn off the machine
        popcorn.off();
        lights.dim(); // Dim light
    }

    public void play() {
        player.play();
    }

    public void pause() {
        player.pause();
    }

    public void end() {
        player.off();
        projector.off();
        stereo.off();
        lights.bright();
        screen.up();
    }
}

[Client]

public class Client {
    public static void main(String[] args) throws InterruptedException {
        HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
        System.out.println("===========Home theater initialization============");
        homeTheaterFacade.ready();
        System.out.println("===========Immersive playback in home theater============");
        homeTheaterFacade.play();
        Thread.sleep(1000);
        System.out.println("===========Home theater suspended============");
        homeTheaterFacade.pause();
        Thread.sleep(1000);
        System.out.println("===========Immersive playback in home theater============");
        homeTheaterFacade.play();
        Thread.sleep(1000);
        System.out.println("===========Home theater end============");
        homeTheaterFacade.end();
    }
}

4. MyBatis framework source code analysis

The appearance mode is used in MyBatis Configuration to create MetaObject objects

code analysis

Sketch Map

5. Precautions and details of appearance mode

  • 1) The appearance mode shields the details of the subsystem, so the appearance mode reduces the complexity of the client's use of the subsystem
  • 2) The coupling relationship between the client and the subsystem in the appearance mode makes the modules inside the subsystem easier to maintain and expand
  • 3) The rational use of appearance patterns can help us better divide the access levels
  • 4) When the system needs hierarchical design, Facade mode can be considered
  • 5) When maintaining a large legacy system, the system may have become very difficult to maintain and expand. At this time, we can consider developing a Facade class for the new system to provide a clear and simple interface of the legacy system, so that the new system can interact with the Facade class and improve reusability
  • 6) You can't use too many or unreasonable appearance patterns. It's better to use appearance patterns or call modules directly. The purpose is to make the system hierarchical and conducive to maintenance

Tags: Design Pattern

Posted on Sun, 05 Dec 2021 07:34:36 -0500 by jmosterb