Intermediary mode, English name: Mediator.
Basic concept: Intermediaries allow us to expose a unified interface through which different parts of the system can communicate.
Usage scenario: There seems to be too much direct relationship between the components of a system, and a central control point is needed so that the individual components can communicate through the central control point.
Role: Ensure that the interaction of components is handled through this central control point, rather than by explicitly referencing each other. This mode can help us decouple the system and improve the reusability of the components.
Specific scenario: The airport control tower (intermediary or central control point) handles the takeoff and landing of the aircraft, and the communication between the planes is delivered through the airport control tower, not between the aircraft and the aircraft.
As an example:
The implementation of the event delegate under the DOM event bubbling mechanism.
If all the events in our Web application are bound (subscribed) to document documents instead of node nodes, then this document will effectively act as a mediator. Because all event firings are passed through the document to the object in the callback method. The element that triggers the event (Button,input, and so on) communicates with the object in the callback method through document.
The biggest advantage of the broker pattern is the ability to reduce the number of communication channels required between objects or components in the system from many-to-many to many-to-one.
The difference between the mediator pattern and the Observer pattern: Although both the mediator pattern and the observer pattern can facilitate code decoupling, the mediator pattern implements code decoupling by restricting the object to communicate strictly through mediator, and the observer pattern is created by creating the Observer object, It also allows the Observer object to subscribe to the target object for decoupling of the code.
Prototype mode, English name: Prototype
Prototype mode is based on the pattern of prototype inheritance, we can create an object, and then use this object as a prototype of other objects.
A prototype object is actually a blueprint (template) that is used as a constructor to create each instance object. If the prototype of the constructor used contains a Name property, then each object created by this constructor will also have the same properties.
In ECMAScript5, there is a method object.create (), which returns an object, receives two parameters, the first parameter is the prototype object to return the object, and the second parameter is an additional Property object that returns an object (optional).
As an example:
var vehicle = {Getmodel:function () {console.log (This.model);}}; var car = object.create (vehicle, {"model": "Chaojidan"}); Car.getmodel (); Print out "Chaojidan"
You may not understand the internal implementation of the Object.create method, but it is simple:
Object.create = function (pro,obj) {function f () {};f.prototype = Pro;var F = new F (); for (var i in obj) { //This code assumes the properties of the Obj object Values are string f.i = obj.i;} return F;}
Prototype patterns are easy to understand, and the prototype chain has to be mastered, whether it's an interview or writing code, which is very useful.
Come on!
Mediator mode and prototype mode