Appearance mode description
Note: Appearance mode is a high-level interface interface provided by a complex subsystem or program composition, making it easier to access the underlying program or system interface using the client;
Appearance mode is a pattern we often encounter. The functions we often involve may involve several subinterfaces or subsystems, while a certain function of ours may only require this to be encapsulated in the order of one or several of multiple subinterfaces. If the business function directly corresponds to the subinterface or subsystem, the developer may need to have a considerable understanding of the internal content; you may need to understand how the business process goes, what its order is, etc. This requires developers to understand the business and also makes client programming quite complicated;
If there is a layer or a class here that specifically provides the method we want to use to encapsulate it, the client function only needs to interact with this intermediate layer class, and the corresponding methods of the intermediate layer class have related developers who understand the business organization encapsulation, then the program will become very simple. The programmer only needs to know which method is required for his function, and does not need to know the internal logic.
This middle layer class is what we call the appearance class, which is the idea of appearance pattern.
Scene Example:
1>. For example, the master switch, this master switch can control a light on the door of the home, a few lights in the hall, and control the power supply of the home TV, refrigerator, etc. If you press "ON" on, which one will have electricity, and even directly emit light and heat transmission. You don't have to know how the button on the master switch comes out, or how it is pressed to the relevant electrical appliances. Anyway, you will call it directly.
These lights, TVs, etc. are the interfaces and small systems we want to use; this master switch is our appearance, and we can operate it directly.
2>. It’s a good thing that a company has several functional departments. When the boss needs the implementation of various aspects of work one by one, he will go to the department and ask an employee how this thing is doing. If he asks the right person, he can directly answer the boss. If this person is responsible, he will also tell the boss, "Oh, who is responsible for this matter, and the boss has to run to ask the person, it's so troublesome."
If each functional department has a supervisor, the boss can just go to it and find out the situation. The boss doesn’t have to care about how the person in charge knows this. He just wants to know the situation of 1, 2, and 3 things and follow the progress.
Instance source code
Now implement the source code according to the second instance scenario:
1. Several departmental functions:
Department 1 (Business Department):
The code copy is as follows:
function BusinessDept() {
this.manager = 'Manager Chen'; //People in charge
}
BusinessDept.prototype = {
MonthSales: function() {
console.log(this.manager + 'Say: This month's sales are xxx');
},
NextPlan: function() {
console.log(this.manager + 'Say: The next plan is like this, xxxx');
}
}
Department 2 (R&D department):
The code copy is as follows:
function RDdept() {
this.manager = 'Manager Huang';
}
RDdept.prototype = {
progress: function() {
console.log(this.manager + 'Say: The current project situation and progress are like this xxx');
},
deptPlan: function() {
console.log(this.manager + 'Say: The next department plan is like this xxx');
}
}
The above are the questions the supervisors of each department have to answer the boss;
Next, establish the appearance category to organize the questions your boss wants to ask;
The code copy is as follows:
function Facade() {
this.business = new BusinessDept();
this.rddept = new RDdept();
}
Facade.prototype = {
DeptSituation: function() {
this.business.MonthSales(); //The sales manager will say first;
this.rddept.progress();
},
deptPlan: function() {
this.business.NextPlan(); //Report the next plan;
this.rddept.deptPlan();
}
}
Next, the boss called the two managers in front of him and started to ask questions:
The code copy is as follows:
var facade = new Facade();
console.log('Boss asked: Now introduce the situation of your department?');
facade.DeptSituation();
console.log('Boss asked: What are the plans next?');
facade.deptPlan();
Other Instructions
Using appearance mode can make interfaces or classes decoupled, so that there is no need to dependence between classes. When it is unnecessary to use, A must include B, or B must include A. This violates the principle of closing modification. Using intermediate layer appearance class wrappers can make interface calls simple, and using subinterfaces or subsystem object calls become more free and organized.
Appearance mode often appears in our programming, and appearance mode is often used in the pattern definition of the architecture system. Our system needs to use third-party interface services, and often add an appearance layer to organize available business interfaces;