@TOC

1 cinema management project

Requirements: Set up a home theater: DVD player, projector, automatic screen, surround sound, popcorn maker, requirements to complete the use of home theater functions, the process is: directly with the remote control: Turn on the popcorn machine, put down the screen, turn on the projector, turn on the stereo, turn on the DVD, select the DVD, get the popcorn, dim the lights and play the video, turn off the equipment after watching the movieCopy the code

2 traditional way to solve theater management

3. Analysis of traditional ways to solve the problems of cinema management

(1) In ClientTest's main method, creating objects of each subsystem and directly calling methods related to subsystems (objects) will cause confusion in the calling process and no clear process. (2) It is not conducive to maintaining operations on sub-systems in ClientTest. (3) Solution: define a high-level interface to provide a consistent interface for a group of interfaces in the subsystem (such as providing four methods in the high-level interface). (Ready, Play, Pause, end), used to access a group of interfaces in a subsystem. (4) That is to say, by defining a consistent interface (interface class), to shield the details of the internal subsystem, so that the calling end only needs to issue calls with this interface, without caring about the internal details of the subsystem => appearance mode.Copy the code

4 Appearance Mode Introduction

Basic introduction

(1) Facade, also known as "process pattern: Appearance model for the subsystem of a set of interfaces provide a consistent interface * * * *, this pattern defines a high-level interface, this interface makes the subsystem easier to use (2) the appearance model by defining a consistent interface, with * * * *, with mask internal subsystem of details make * * calling end only happen with the interface call * *, You don't have to worry about the inner details of this subsystemCopy the code

5 Appearance mode Schematic class diagram

Class diagram description (roles for categorizing appearance patterns)

(1) Facade: a unified invocation interface is provided for the calling end. The Facade class knows which subsystems are responsible for processing requests, so as to delegate the requests of the calling end to appropriate subsystem objects. (2) Caller (Client): caller of the appearance interface. (3) Collection of subsystems: modules or subsystems that handle tasks assigned by Facade objects and are the actual providers of functionality.Copy the code

6 Appearance Mode Solve theater management

6.1 Traditional Theater Management

(1) The appearance mode can be understood as transforming a group of interfaces. Customers only need to call one interface instead of multiple interfaces to achieve the purpose. For example, when installing software on a PC, there is often a one-click installation option (eliminating the need to select the installation directory, installed components, etc.), as well as the restart function of the phone (shutdown and startup combined into one operation). (2) Appearance mode is to solve the difficulty of using multiple complex interfaces and simplify user operations. (3) Schematic illustrationCopy the code

6.2 Application Examples of Appearance Mode

6.2.1 Train of Thought Analysis and Diagram (Class Diagram)

6.2.2 Code implementation

HomeTheaterfacade class

Public class HomeTheaterFacade {// Define subsystem objects private TheaterLight TheaterLight; private Popcorn popcorn; private Screen screen; private DVDPlayer dVDPlayer; public HomeTheaterFacade() { this.theaterLight = TheaterLight.getInstance(); this.popcorn = Popcorn.getInstance(); this.screen = Screen.getInstance(); this.dVDPlayer = DVDPlayer.getInstanc(); Public void ready() {popcorn.on(); popcorn.pop(); screen.down(); dVDPlayer.on(); theaterLight.dim(); } public void play() { dVDPlayer.play(); } public void end() { popcorn.off(); theaterLight.bright(); screen.up(); dVDPlayer.off(); }}Copy the code

Other class 1

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 "); }}Copy the code

Other class 2

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.. "); }}Copy the code

Other class 3

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 "); }}Copy the code

Other class 4

Private static DVDPlayer instance = new 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 "); }} need complete code, leave email in the comment areaCopy the code

The test class

public class Client { public static void main(String[] args) { HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(); homeTheaterFacade.ready(); homeTheaterFacade.play(); homeTheaterFacade.end(); Popcorn on popcorn is poping Screen down DVD on TheaterLight dim.. dvd is playing popcorn ff TheaterLight bright.. Screen up dvd offCopy the code

7 Appearance mode notes and details

(1) Appearance mode ** shields the details of subsystems **, so appearance mode reduces the complexity of client's use of subsystem. (2) Appearance mode decouples the coupling relationship between client and subsystem, making it easier to maintain and expand the modules inside subsystem. (3) By using appearance mode properly, (4) When a system needs to be layered, consider using the Facade pattern. (5) When maintaining a large legacy system, it may become very difficult to maintain and extend, 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 the reuse. (6) Do not use the Facade mode too much or unreasonable, it is better to use the Facade mode, or directly call modules. To let the system have layers, conducive to maintenance for the purpose.Copy the code

8 appearance mode in MyBatis framework application source analysis

1) Configuration in MyBatis to create MetaObject objects using appearance mode

public class Configuration { protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory(); protected ObjectFactory objectFactory = new DefaultObjectFactory(); protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory(); public MetaObject newMetaObject(Object object) { return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory); }}Copy the code
Instructions: The rest to be added