Application scenarios
To complete a function, the user needs to call multiple interfaces and methods of the provider. When the calling process is complex, we can provide a high-level interface (new appearance) to hide the complex calling process from the user, which is a variation of the adapter mode
The appearance model
define
Hiding the complexity of the system and providing an interface through which the client can access the system
intentions
To provide a consistent interface for a set of interfaces in a subsystem, the facade defines a high-level interface that makes the subsystem easier to use
Main problem solving
Reduce the complexity of accessing the internal subsystem of a complex system and simplify the interface between the client and it
When to use
- The client does not need to know the complex connections within the system, the whole system only needs to provide a “receptionist”
- Define the entry to the system
The advantages and disadvantages
Advantages:
- Reduce system interdependence
- Improve flexibility
- Improved security
Disadvantages: inconsistent with the principle of open and close, if you want to change things is very troublesome, inheritance rewrite are not appropriate
structure
Roles involved:
- Facade role: Methods that can be invoked by clients on this role. This role knows the functions and responsibilities of the relevant subsystems, and under normal circumstances, this role delegates all requests from clients to the appropriate subsystem
- SubSystem role: Can have one or more subsystems at the same time. Each subsystem is not a single class, but a collection of classes; Each subsystem can be called directly by the client, or by the facade role. The subsystem does not know the facade exists, and to the subsystem, the facade is just another client
Security Guard’s Story
As a security guard, need to be familiar with security system, now use security systems have two video recorder, three electric light, a sensor and an alarm, security systems operators need to often the equipment startup and shutdown, first of all, in the case of not using the facade pattern, these parts are need to operating personnel operation directly, write a small example below:Operator class:
public class Client {
static Camera camera1 = new Camera(), camera2 = new Camera();
static Light light1 = new Light(), light2 = new Light(), light3 = new Light();
static Sensor sensor = new Sensor();
static Alarm alarm = new Alarm();
public static void main(String[] args) {
camera1.turnOn("1号录像机");
camera2.turnOn("2号录像机");
light1.turnOn("Lamp 1");
light2.turnOn("No. 2 light");
light3.turnOn("No. 3 light"); sensor.activate(); alarm.activate(); }}Copy the code
Instrument categories:
public class Camera {
public void turnOn(String code) {
System.out.println("Turn on the VCR:" + code);
}
public void turnOff(String code) {
System.out.println("Turn off the VCR:" + code);
}
public void rotate(String code) {
System.out.println("Turn the VCR:"+ code); }}Copy the code
public class Light {
public void turnOn(String code) {
System.out.println("Turn on the light:" + code);
}
public void turnOff(String code) {
System.out.println("Turn off the lights:" + code);
}
public void changeBulb(String code) {
System.out.println("Change a light bulb:"+ code); }}Copy the code
public class Sensor {
public void activate(a) {
System.out.println("Activate sensor");
}
public void deactivate(a) {
System.out.println("Turn off the sensor.");
}
public void trigger(a) {
System.out.println("Trigger the sensor"); }}Copy the code
public class Alarm {
public void activate(a) {
System.out.println("Activate the alarm");
}
public void deactivate(a) {
System.out.println("Turn off the alarm.");
}
public void ring(a) {
System.out.println("Sound the alarm.");
}
public void stopRing(a) {
System.out.println("Stop the alarm."); }}Copy the code
The following changes use facade mode:Add a facade character class:
public class SecurityFacade {
Camera camera1 = new Camera(), camera2 = new Camera();
Light light1 = new Light(), light2 = new Light(), light3 = new Light();
Sensor sensor = new Sensor();
Alarm alarm = new Alarm();
public void activate(a) {
camera1.turnOn("1号录像机");
camera2.turnOn("2号录像机");
light1.turnOn("Lamp 1");
light2.turnOn("No. 2 light");
light3.turnOn("No. 3 light");
sensor.activate();
alarm.activate();
}
public void deactivate(a) {
camera1.turnOff("1号录像机");
camera2.turnOff("2号录像机");
light1.turnOff("Lamp 1");
light2.turnOff("No. 2 light");
light3.turnOff("No. 3 light"); sensor.deactivate(); alarm.deactivate(); }}Copy the code
Modifying the Client class:
public class Client {
public static void main(String[] args) {
SecurityFacade facade = newSecurityFacade(); facade.activate(); }}Copy the code
Class diagram: