preface
Only a bald head can be strong
Review the design patterns I wrote earlier:
-
Explain the agency model to your girlfriend
-
The packaging pattern is as simple as that
-
How many ways do you know how to write singleton?
-
Do you understand the factory model?
-
The strategy model is that simple!
Design patterns are a must for both job interviews and personal promotion. Today we are going to explain the appearance model
Why Alibaba prevents engineers from directly using the API in logging system (Log4j, Logback)
[Mandatory] Applications must rely on the API of SLF4J rather than the API of logging system (Log4j and Logback). Using the facade logging framework helps maintain and unify the log processing modes of each class.
I don’t know if you are familiar with the facade pattern, but I went to the Zen of Design Patterns and found it very simple, so I will share it with you here.
I. Introduction of facade (appearance) mode
1.1 Practical examples of facade mode
A power main switch can control all electrical appliances on or off.
No matter air conditioning, refrigerator, TV, computer, fan and so on, as long as the electrical appliances are controlled by this switch. If this switch is to be turned off, all appliances will be affected.
The main power switch (switch) is designed for the appearance mode of the system.
1.2 Back to the code world
For example, our home now has air conditioning, refrigerator, computer so several electrical appliances
/ / refrigerator
public class Fridge {
// Close the refrigerator
public void turnOff(a) {}// Turn on the refrigerator light.. Reduce the refrigerator temperature.. Turn up the fridge temperature...
}
/ / TV
public class Television {
// Turn off the TV
public void turnOffTV(a) {
System.out.println("Turn off the TV");
}
// Switch the TV program.. Turn down the TV... Turn up the TV...
public void doSomething(a) {
System.out.println("Switch TV programs.. Turn down the TV... Turn up the TV..."); }}/ / computer
public class Computer {
// Close the computer
public void turnOffComputer(a) {
System.out.println("Turn off your computer");
}
// Use the computer for other things ~
public void doSomething(a) {
System.out.println("Use the computer for other things ~"); }}Copy the code
If there is no switch, I want to turn off the above electrical appliances, I need to do:
// I want to turn off the TV, computer and air conditioner
public static void main(String[] args) {
new Computer().turnOffComputer();
new Fridge().turnOffFridge();
new Television().turnOffTV();
// Of course, a normal home consists of more than these appliances.
// If ONE day I want to turn off all appliances in my home, I need to repeat the new object and call its turn Offer method
}
Copy the code
One by one is not very troublesome, so we have the switch:
/ / switch
public class ElectricBrake {
private Computer computer = new Computer();
private Fridge fridge = new Fridge();
private Television television = new Television();
// Turn off all appliances
public void turnOffAll(a) { computer.turnOffComputer(); fridge.turnOffFridge(); television.turnOffTV(); }}Copy the code
When we want to turn off all electrical appliances, we can use the switch to turn them off.
// I want to turn off all electrical appliances
public static void main(String[] args) {
ElectricBrake brake = new ElectricBrake();
brake.turnOffAll();
}
Copy the code
Experienced students might think, isn’t that just another layer of encapsulation? This is the facade mode?? Well, yes, that’s the facade model
1.3 Introduction to the facade Mode
Zen of Design Patterns:
It is required that communication between the outside and the inside of a subsystem must be carried out through a unified object. Facade mode provides a high-level interface that makes subsystems easier to use.
The generic class diagram for facade patterns is simple:
In our case, the subsystem is the computer, refrigerator, TV. The unified object is our electrical switch. We turn everything off by switching off (so we don’t have to go through the computer, refrigerator, TV to turn it off)
Facade mode is used to make client calls easier!
1.4 Advantages and disadvantages of facade mode
Advantages:
-
Reduce system interdependence. With the facade pattern, all dependencies are on the facade object, independent of the subsystem
-
Increased flexibility. No matter what happens inside the subsystem, as long as it doesn’t affect the facade object, you can do whatever you want.
Disadvantages:
- Inconsistent open and close principle, closed for modification, open for expansion. For example, in our example above, if a new appliance wants to join the shutdown queue, it can only be modified on the facade object
turnOffAll()
Method code.
The last
Do you think the facade design pattern is such a thing? To put it bluntly, the subsystem encapsulates a layer and gives a high-level interface (facade object), which is convenient for clients to call.
Recommended reading and references:
-
Zen of Design Patterns
-
Blog.csdn.net/hguisu/arti…
-
www.cnblogs.com/lthIU/p/586…
Happy to share and export dry Java technology public account: Java3y. Pay attention to receive massive video resources!
The text has been included in my GitHub warehouse, welcome Star:
- Github.com/ZhongFuChen…