Java Facade

1, Lead to the question of building a home theater:

There are DVD player, projector, automatic screen, surround sound and popcorn machine in the home theater. It is required to complete the function of using the 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 audio
• open DVD, select DVD
• get popcorn
• dim the lights
• play
• after viewing, turn off all kinds of equipment

2, Using traditional methods to solve Cinema Management


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, provide four methods ready, play, pause and end in the high-level interface) to access a group of interfaces in the subsystem.
  4. In other words, define a consistent interface (interface class) to shield the details of the internal subsystem, so that the caller only needs to call the interface without paying attention to the internal details of the subsystem. At this time, use the appearance mode to solve it.

3, Basic introduction to appearance mode

  1. 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, which makes the subsystem easier to use.
  2. Appearance mode defines a consistent interface to shield the details of the internal subsystem, so that the caller only needs to call the interface without paying attention to the internal details of the subsystem.

Schematic class diagram:

Description of schematic class diagram (role of appearance mode)

  1. Facade: provide 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. The set of sub-system tasks assigned by the Facade refers to the set of other sub-system tasks.

4, Appearance mode to solve Cinema Management

Description of solving Cinema Management in traditional way

  1. The appearance mode can be understood as converting a group of interfaces. Customers can achieve the goal only by calling one interface instead of multiple interfaces. For example, when installing software on a pc, there are often one click installation options (omitting the selection of installation directory, installed components, etc.) and the restart function of the 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 operation
  3. Schematic illustration

    Code implementation:
public class DVDPlayer {	
	//Use singleton mode and hungry Han style
	private static DVDPlayer instance = new DVDPlayer();
	public static DVDPlayer getInstanc() {
		return instance;
	}
	public void on() {
		System.out.println(" dvd on ");
	}
	public void off() {
		System.out.println(" dvd off ");
	}
	public void play() {
		System.out.println(" dvd is playing ");
	}
	public void pause() {
		System.out.println(" dvd pause ..");
	}
}
public class Popcorn {
	private static 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 ff ");
	}	
	public void pop() {
		System.out.println(" popcorn is poping  ");
	}
}
public class Projector {
	private static Projector instance = new Projector();	
	public static Projector getInstance() {
		return instance;
	}	
	public void on() {
		System.out.println(" Projector on ");
	}
	public void off() {
		System.out.println(" Projector ff ");
	}
	public void focus() {
		System.out.println(" Projector is Projector  ");
	}
}
public class Screen {
	private static Screen instance = new Screen();
	public static Screen getInstance() {
		return instance;
	}
	public void up() {
		System.out.println(" Screen up ");
	}	
	public void down() {
		System.out.println(" Screen down ");
	}	
}
public class Stereo {
	private static Stereo instance = new Stereo();
	public static Stereo getInstance() {
		return instance;
	}	
	public void on() {
		System.out.println(" Stereo on ");
	}
	public void off() {
		System.out.println(" Screen off ");
	}
	public void up() {
		System.out.println(" Screen up.. ");
	}
}
public class TheaterLight {

	private static TheaterLight instance = new TheaterLight();
	public static TheaterLight getInstance() {
		return instance;
	}
	public void on() {
		System.out.println(" TheaterLight on ");
	}
	public void off() {
		System.out.println(" TheaterLight off ");
	}
	public void dim() {
		System.out.println(" TheaterLight dim.. ");
	}
	public void bright() {
		System.out.println(" TheaterLight bright.. ");
	}
}
public class HomeTheaterFacade {
	//Define each subsystem object
	private TheaterLight theaterLight;
	private Popcorn popcorn;
	private Stereo stereo;
	private Projector projector;
	private Screen screen;
	private DVDPlayer dVDPlayer;	
	//constructor 
	public HomeTheaterFacade() {
		super();
		this.theaterLight = TheaterLight.getInstance();
		this.popcorn = Popcorn.getInstance();
		this.stereo = Stereo.getInstance();
		this.projector = Projector.getInstance();
		this.screen = Screen.getInstance();
		this.dVDPlayer = DVDPlayer.getInstanc();
	}
	//The operation is divided into four steps	
	public void ready() {
		popcorn.on();
		popcorn.pop();
		screen.down();
		projector.on();
		stereo.on();
		dVDPlayer.on();
		theaterLight.dim();
	}	
	public void play() {
		dVDPlayer.play();
	}	
	public void pause() {
		dVDPlayer.pause();
	}	
	public void end() {
		popcorn.off();
		theaterLight.bright();
		screen.up();
		projector.off();
		stereo.off();
		dVDPlayer.off();
	}
}
public class Client {
	public static void main(String[] args) {
		//Directly call here.. so much trouble
		HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
		homeTheaterFacade.ready();
		homeTheaterFacade.play();	
		homeTheaterFacade.end();
	}

}

Result diagram:

Final summary:
When the customer only needs to call one interface instead of calling multiple interfaces, the operation can be completed. For example, when installing software on a pc, there are often one click installation options (omitting the selection of installation directory, installed components, etc.), as well as the restart function of the mobile phone (combining shutdown and startup into one operation). At this time, the appearance mode is used. When there are many subsystems, it is more troublesome for users to call subsystems. At this time, define a Facade class to encapsulate the operations of all subsystems into this class. At this time, user calls are more concise. Appearance mode is to solve the difficulties caused by multiple complex interfaces and simplify user operation.

Tags: Java Programming Design Pattern

Posted by intenseone345 on Wed, 25 May 2022 09:09:47 +0300