One, general overview
1, the author discusses
Speaking of "façade" This design mode in fact, regardless of the new and old program apes are inadvertently applied to this mode, like our beautiful JS programmer inadvertently used the closure processing problem,
1 function Employee (name) {2 var name = name; 3 This function () {4 console.log ("I am Employee" + name); 5 } ; 6 }
The code "say" is a function,employee is also a function this constitutes a closure environment, in fact, most of us are above the definition of the way, also imperceptible use of closures, O (∩_∩) o~ let us continue to come back, the façade of this model is easy to understand see as
The figure is clearly described, three subsystems independent of each other has its own responsibility division, from the client's point of view is not concerned about their details, then how to connect the three subsystems with the client? Good!!! Then the protagonist of the article should be unveiled, this is the façade mode it plays a unified entrance duty,
Reduce the dependencies between the client and the implementation layer between subsystems. When building a hierarchical system, you can also use the facade pattern to define the ingress of each layer in the system, simplifying the dependency between layers.
The text may be somewhat abstract but slowly, step by step analysis, now assume that there is such a scene, there are two to complete the plug-in module, based on this two times development, we think this is similar to the scene in the diagram, the following is the analog code to help understand
1 functionHelloWorld () {2 This. Greetplug =NewGreetplug ();3 This. Filterplug =NewFilterplug ();4 This. Hello =function(name) {5 if(! This. Filterplug.filter (Name, "Zhangsan")){6 This. Greetplug (). Say (name);7 }8 }9 } ;Ten functionGreetplug () { One This. Say =function(what) { AConsole.log ("Hello," +what ); - } - } ; the functionFilterplug () { - This. Filter =function(what,not) { - return(what = = = not)?true:false ; - } +} ;
Inside the Helloeorld is relative to and façade interface, as for the two outer two classes are hidden, this is the idea! Take a look at two more application examples to deepen understanding, such as
Hides the details of the DOM standard event interface and makes the event-compatible API.
This everyone is more familiar with, jquery Ajax operation is normal operation is relatively frequent, yes it also used the façade mode, the bottom layer is the low-level API to do support, façade layers through different parameters division function!
Second, façade and Dimitri law
The Dimitri rule (law of Demeter) is also called the least knowledge principle (Least knowledge Principle shorthand Lkp), that is, one object should have as little understanding of other objects as possible and not speak to strangers. The English shorthand is: LoD.
The core concept of Dimitri Law is the decoupling between classes, weak coupling, and the reusability of classes can be improved only after weak coupling.
In fact, the relationship between the two is very good understanding, the façade mode is based on this law derived from the solution, suitable for a particular scenario of the same kind of problem solution. Let's take a look at the following example:
Now there is a scene------prison inmates should not contact the outside people, of course, may have relatives. The prison here is the class, inside the prisoner is the information inside the class, and the prison guards are equivalent to the Dimitri law of the executor
1 functioninmates () {2 This. Say =function(){3Console.log ('the inmate said: "We are prison friends ...");4 } ;5 } ;6 function Prisoners () {7 this.inmates = new inmates ();8 this.helpeachother = function () {9 This.inmates.say ();Ten Console.log ("The family said: you and the inmates should help each other ..."); One } ; A } ; - function Family () { - this.prisoners = new Prisoners (); the this.visit = function (inmates,prisoners) { - This.prisoners.helpEachOther (); - } ; -} ;
Seeing such a result, is not a bit awkward, the family told the prisoner to be friendly with prison, and the inmates did come out to talk. This clearly crosses the border, because the prison only allows family members to visit the inmates, not just anyone can see
The family and the inmates here are communicating against the Dimitri law, so we need to isolate the family and inmates and reconstruct it.
(1), define home human
1 function Family () {2 This New prisoners (); 3 This function (prisoners) {4 console.log ("The family said:"); 5 This . Prisoners.helpeachother (); 6 } ; 7 };
(2), defining the crime of human
1 function prisoners () {2 This New inmates (); 3 This function () {4 console.log ("Prisoners and inmates should help each other ..."); 5 Console.log ("The prisoner said:"); 6 inmates.wearefriends (); 7 } ; 8 };
(3), define the prison friend class
1 function inmates () {2 This function () {3 console.log (' We are inmates ... '); 4 } ; 5 };
(4), defining the scene class
1 function Prison () { 2 new Family (). Visit (new Prisoners ()); 3 };
So the family and inmates separated, but also expressed the family hope that inmates can help each other with the intention of the inmates. That is, two classes implement information passing through a third class
Three, Summary summary
Summary of Dimitri Law:
The Dimitri law does not want the class directly to establish direct contact. If there really is a need to establish a connection, it is also hoped that it will be conveyed through its friend class. Therefore, one of the possible consequences of applying the Dimitri law is that there are a large number of intermediary classes in the system,
These classes exist solely to pass the mutual invocation relationship between classes------which increases the complexity of the system to some extent. It is interesting to study the design pattern of the façade mode (facade) and the mediation Mode (mediator),
Are examples of the application of the Dimitri Law.
Advantages of the façade mode:
Loosely coupled
The façade mode is loosely coupled between the client and subsystem, allowing the modules inside the subsystem to be more easily extended and maintained.
Simple to use
The façade mode makes the subsystem more easy to use, the client no longer needs to understand the implementation within the subsystem, and does not need to interact with the modules inside the many subsystems, just interact with the façade class.
Better partitioning of access levels
By using facade wisely, you can help us to better classify the level of access. Some methods are external to the system, and some methods are used internally by the system. The ability to expose external functionality to the façade, which makes it easy for clients to use and hide internal details well.
hahaha, the end of this article, not to be continued, hope and we have enough communication, common progress (*^__^*) hehe ...
Big Xun Jun talk about JS and design mode (façade mode facade) Dimitri Law of redemption------(prison story)