Cinema Management Project

Set up a home theater: DVD player, projector, automatic screen, surround sound, popcorn maker, required to complete the use of home theater functions, the process is as follows

  • Direct remote control: coordinate the switch of each device
  • Popcorn opener
  • Put down the screen
  • A projector
  • Open the sound
  • Open DVD, select DVD
  • Get the popcorn.
  • Dim the lights
  • play
  • After watching the movie, turn off all devices

Plan 1Pseudo code:

Public static void main(){//1; //2; //2; //3;Copy the code

Problem analysis:

  • In ClientTest’s main method, creating objects of each subsystem and directly calling methods related to subsystems (objects) will cause confusion in the call process and no clear process.
  • It is not conducive to maintaining operations on sub-systems in ClientTest.
  • Use the appearance mode to define a high-level interface that provides a consistent interface for a group of interfaces in the subsystem (for example, provide four methods ready, Play, Pause, and end on the high-level interface). Used to access a set of interfaces in a subsystem.

Appearance mode basic introduction

  • Appearance pattern: Also known as process pattern, appearance pattern provides a consistent interface for a set of interfaces in a subsystem. This pattern defines a high-level interface that makes the subsystem easier to use.
  • The facade hides the details of the internal subsystem by defining a consistent interface (interface class) so that the caller only needs to make calls to the interface without having to deal with the internal details of the subsystem.

The principle of class diagram

Description:

  • Facade class: Provides a unified invocation interface for the calling side, and the Facade class knows which subsystems are responsible for processing the request, thus proxy the request from the calling side to the appropriate subsystem objects.
  • Caller (Client) : Caller of the facade interface
  • SubSystem: A module or SubSystem that handles the tasks assigned by the facade object, which is the provider of functionality.

External mode addresses theater management projects

Class diagram:

Code:

public class Facade { private DVDPlayer dvdPlayer; private Projector projector; private Screen screen; private Stereo stereo; private Popcorn popcorn; private TheaterLight theaterLight; public Facade() { this.dvdPlayer = DVDPlayer.getInstance(); this.projector = Projector.getInstance(); this.screen = Screen.getInstance(); this.stereo = Stereo.getInstance(); this.popcorn = Popcorn.getInstance(); this.theaterLight = TheaterLight.getInstance(); } public void ready() { popcorn.on(); dvdPlayer.on(); screen.down(); projector.on(); projector.focus(); stereo.on(); theaterLight.dim(); popcorn.pop(); } public void play() { dvdPlayer.play(); } public void pause() { dvdPlayer.pause(); } public void end() { dvdPlayer.off(); screen.up(); projector.off(); popcorn.off(); theaterLight.bright(); stereo.off(); } } class DVDPlayer { private DVDPlayer() { } private static final DVDPlayer dvdPlayer = new DVDPlayer(); public static DVDPlayer getInstance() { return dvdPlayer; } public void on() { System.out.println("DVDPlayer on..." ); } public void off() { System.out.println("DVDPlayer off..." ); } public void pause() { System.out.println("DVDPlayer pause..." ); } public void play() { System.out.println("DVDPlayer play..." ); } } class Projector { private Projector() { } private static final Projector projector = new Projector(); public static Projector getInstance() { return projector; } public void on() { System.out.println("Projector on..." ); } public void off() { System.out.println("Projector off..." ); } public void focus() { System.out.println("Projector focus..." ); } } class Screen { private Screen() { } private static final Screen screen = new Screen(); public static Screen getInstance() { return screen; } public void up() { System.out.println("Screen up..." ); } public void down() { System.out.println("Screen down..." ); } } class Popcorn { private Popcorn() { } private static final Popcorn instance = new Popcorn(); public static Popcorn getInstance() { return instance; } public void on() { System.out.println("Popcorn on..." ); } public void off() { System.out.println("Popcorn off..." ); } public void pop() { System.out.println("Popcorn pop..." ); } } class Stereo { private Stereo() { } private static final Stereo instance = new Stereo(); public static Stereo getInstance() { return instance; } public void on() { System.out.println("Stereo on..." ); } public void off() { System.out.println("Stereo off..." ); } } class TheaterLight { private TheaterLight() { } private static final TheaterLight instance = new TheaterLight(); public static TheaterLight getInstance() { return instance; } public void dim() { System.out.println("TheaterLight dim..." ); } public void bright() { System.out.println("TheaterLight bright..." ); }}Copy the code
public class FacadeTest { public static void main(String[] args) { Facade facade = new Facade(); facade.ready(); facade.play(); facade.pause(); facade.play(); facade.end(); }}Copy the code

Appearance mode considerations and details

  • Facade mode hides the details of the subsystem from the outside world, so facade mode reduces the complexity of the client’s use of the subsystem.
  • The facade decouples the client from the subsystem, making the modules inside the subsystem easier to maintain and extend.
  • The proper use of appearance patterns can help us better divide the access hierarchy.
  • When a system needs to be layered, consider using appearance patterns.
  • When maintaining a large legacy system, it may become very difficult to maintain and extend the system. In this case, we can consider developing a Facade class for the new system to provide a relatively clear and simple interface to the legacy system, so that the new system can interact with the Facade class and improve the reuse.
  • Do not use the appearance mode too much or unreasonable, it is better to use the appearance mode, or directly call the module, in order to make the system hierarchical, conducive to maintenance for the purpose.