Appearance mode (process mode)

Theater management project


Set up a home theater: DVD player, projector, automatic screen, surround sound, popcorn machine, it is required to complete the function of using the home theater, the process is:
directly use the remote control: turn on the switch of all devices,
turn on the popcorn machine,
put down the screen and
turn on Projector Turn
on audio , Turn
on DVD, choose DVD to
get popcorn,
dim the light,
play
, turn off all devices after watching the movie

Traditional way to solve theater management

Insert picture description here

Analysis of traditional ways to solve theater management problems

  1. In the main method of ClientTest, create the objects of each subsystem and directly call the relevant methods of the subsystem (object), which will cause confusion in the calling process, and there is no clear process.
  2. It is not conducive to maintaining the operation of the subsystem in ClientTest
  3. Solutions: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, end are provided in the high-level interface) to access a group of interfaces in the subsystem
  4. That is to say, by defining a consistent interface (interface class) to shield the details of the internal subsystem, so that the caller only needs to call this interface without worrying about the internal details of the subsystem => appearance mode

Basic introduction to appearance mode

basic introduction

  1. Facade, also called "Process mode: The appearance mode provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes this subsystem easier to use
  2. The appearance model defines a consistent interface forShield the details of internal subsystems, MakingThe caller only needs to call this interfaceWithout worrying about the internal details of this subsystem

Schematic diagram of appearance mode

Insert picture description here

  • Description of the class diagram (the role of the classification appearance mode)
  1. Facade: Provides a unified calling interface for the caller. The facade class knows which subsystems are responsible for processing the request, so that the caller's request is delegated to the appropriate subsystem object

  2. Caller (Client): The caller of the appearance interface

  3. A collection of subsystems: refers to modules or subsystems that handle tasks assigned by Facade objects, and they are the actual providers of functions

Appearance mode solves theater management

The traditional way to solve the theater management instructions

  1. Appearance mode can be understood as converting a group of interfaces. Customers only need to call one interface instead of calling multiple interfaces to achieve the goal. For example, when
    installing software on a PC , there is often a one-click installation option (saving the choice of installation directory, installed components, etc.), and the restart function of the phone (combining
    shutdown and startup into one operation).
  2. Appearance mode is to solve the difficulty of use caused by multiple complex interfaces and play a role in simplifying user operations
  3. Schematic description
    Insert picture description here

Appearance mode application examples

  1. Application example requirements
  2. Use appearance mode to complete home theater projects
  3. Idea analysis and illustration (class diagram)
    Insert picture description here
  4. Code

HomeTheaterFacade.java: large system

public class HomeTheaterFacade {
    
    
	
	//定义各个子系统对象
	private TheaterLight theaterLight;
	private Popcorn popcorn;
	private Stereo stereo;
	private Projector projector;
	private Screen screen;
	private DVDPlayer dVDPlayer;
	
	
	//构造器
	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();
	}

	//操作分成 4 步
	
	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();
	}
	
}

DVDPlayer.java: Subsystem

public class DVDPlayer {
    
    
	
	//使用单例模式, 使用饿汉式
	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 ..");
	}
}

Client.java: client

public class Client {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		//这里直接调用。。 很麻烦
		HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
		homeTheaterFacade.ready();
		homeTheaterFacade.play();
		
		
		homeTheaterFacade.end();
	}

}

Source code analysis of appearance mode in MyBatis framework

  1. Configuration in MyBatis to create MetaObject objects to use appearance mode
  2. Code analysis + Debug source code + schematic diagram
    Insert picture description here
  3. The role class diagram of the appearance mode used in the source code
    Insert picture description here

Notes and details of appearance mode

  1. Appearance modeThe details of the subsystem are shielded from the outside, So the appearance mode reduces the complexity of the client’s use of the subsystem

  2. The appearance mode couples the client and the subsystem-decoupling, making the modules inside the subsystem easier to maintain and expand

  3. By using the appearance model reasonably, we can help us betterDivide access levels

  4. When the system needs hierarchical design, you can consider using Facade mode

  5. When maintaining a large legacy system, the system may have become very difficult to maintain and expand. At this time, you 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 and the facade class Interaction, improve reusability

  6. Do not use the appearance mode excessively or unreasonably. It is better to use the appearance mode instead of directly calling the module. The purpose is to make the system hierarchical and facilitate maintenance.

to sum up

Of a large system (class)An operationWill call == many subsystems (classes) ==
so the subsystems are combined into a large system system

Guess you like

Origin blog.csdn.net/weixin_46168350/article/details/111083525