Notes nine design patterns skin mode

Theater Management Project

A home theater set:
DVD player, a projector, automatic screen, surround sound, popcorn machine, required to complete home theater functions, which process is: directly with the remote control: the switching device co-ordinate
opening popcorn machine down screen on stereo projector open
open DVD, selected from dvd
get popcorn dim the lights play
View Movies after closing the various devices

Traditional approaches to solving Theater Management

Here Insert Picture Description

Traditional approaches to solving Theater Management Analysis

  1. In the main method ClientTest, create objects of various subsystems, and direct calls to the subsystems (objects) correlation method, the calling process will result in confusion, there is no clear process
  2. Is not conducive to the ClientTest, the operation of the subsystem to maintain
  3. Solution: Define a high-level interface to a set of subsystem interface provides a consistent interface (such as providing four methods ready, play, pause, end in a high-level interface), the interface used to access a group of subsystems
  4. That is, by defining a consistent interface (interface type) for shielding the internal details of the subsystem, so that the calling terminal simply call with the interface occurs, without caring about the details of the internal subsystem => Appearance Model

Basic introduction to skin mode

basic introduction

  1. Facade pattern (Facade), also known as the "process approach: Facade pattern provides a consistent interface subsystem of a set of interfaces, this model defines a high-level interface that makes the subsystem easier to use
  2. Appearance pattern by defining a consistent interface to shield the internal details of the subsystem, such that only calling terminal calls the interface with this occurs, without caring about the details of the internal subsystem

Patterns for the appearance of FIG class

Here Insert Picture Description

 class diagram illustrating (classification character appearance model)

  1. Appearance category (Facade): to provide a unified call interface for the call ends, the appearance of the class to know which subsystem is responsible for processing requests, which will end the call to the appropriate subsystem object request broker
  2. The caller (Client): the caller appearance of the interface
  3. Collection subsystem: means modules or subsystems, the processing tasks assigned Facade subject who does the actual function of the provider

Facade pattern to solve Theater Management

Traditional approaches to solving Theater Management Description

  1. Appearance conversion mode can be understood as a group of interfaces, customers simply call a port, without invoking multiple interfaces in order to achieve their goals. For example: the pc when installing the software often have a key installation options (eliminating the need to select the installation directory, installation of components, etc.), there is the phone restart function (the shutdown and startup into one operation).
  2. Facade pattern is to solve the difficulties caused by the use of multiple complex interfaces, simplify user operations played a role
  3. Schematically illustrates

Here Insert Picture Description

Appearance Model Application Examples

  1. Examples of application requirements
  2. Use the appearance of models to complete home theater project
  3. Ideas and analysis scheme (FIG class)

Here Insert Picture Description

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();
	}
	
	
}

Analysis of the appearance pattern in the source application framework MyBatis

  1. The Configuration MyBatis to create objects using MetaObject to skin mode
  2. + + Source code analysis the Debug schematic
    Here Insert Picture Description
    Here Insert Picture Description

Notes and details of the mode of appearance

  1. External appearance model details screen subsystem, the appearance of the pattern reduces the complexity of the client subsystem is
  2. Appearance mode coupling relationship between the client and subsystems - decoupled, so that internal subsystem modules easier to maintain and extend
  3. By proper use of the appearance mode, you can help us better division level access
  4. When the system needs to be layered design, consider using the Facade pattern
  5. While maintaining a legacy of large systems, this system may have become very difficult to maintain and extend, then you can consider developing a new system for the
    Facade class, to provide a more clear and simple interface to legacy systems, so that the new system with the Facade class interaction, improve reusability
  6. Not excessive or unreasonable use skin mode, use the facade pattern is good, or directly call good module. To allow the system level, conducive to the maintenance for the purpose.
Published 93 original articles · won praise 31 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43866567/article/details/104803609