In the "JavaScript design mode" about the intermediary model of the introduction, there are some errors and unauthorized additions to the example, although the example (the English version is not) for the sake of a better understanding, but the text added to the example of a misleading feeling, so if someone read this chapter, it is recommended to read the English version.
When I look at this pattern, I just want to figure out, what is the difference between a broker pattern and a subscription/release model?
Broker Pattern Definition
As a behavioral design pattern, a mediator exposes a unified interface through which different objects or components of a system can communicate. When a mediator object is added, all related objects communicate through the Mediator object instead of referencing each other, so when an object changes, only the mediator object is notified.
English version:
When it comes to the mediator and Event aggregator patterns, there is some times where it may look like the patterns is Interchangeable due to implementation similarities. However, the semantics and intent of these patterns are very different.
And even if the implementations both use some of the same core constructs, I believe there is a distinct difference betwee n them. I also believe they should not being interchanged or confused in communication because of the differences.
Chinese version:
It may also be considered an additional or application-wide notification, such as communication between different subsystems, which are inherently complex and may wish to achieve decoupling between internal components through a publish/subscribe relationship.
I:
First of all, I do not understand why the translator does not translate in English, but also the meaning of a little translation is not the English version of the meaning, I feel wrong.
I think it is--the mediator mode and the event subscription pattern sometimes seem to be interchangeable, because they are implemented in the same way, but in terms of semantics and intent, these patterns are different. Even if the implementation uses the same core structure, I believe there is a difference between them, which is not interchangeable and confusing when communicating.
PS: The summary is that the two pattern implementations are somewhat the same, but the intentions are different.
Example
Briefly:
Airport traffic control system. The control tower handles the takeoff and landing of the aircraft, and all communication is controlled by the control tower.
Code:
//AircraftvarPlane =function(name) { This. Name =name;} Plane.prototype.takeOff=function(){//TakeoffControltower.takeoff ( This);} PLANE.PROTOTYPE.SENDMSG=function(Toplane, MSG) {//sending messages between airplanesControltower.sendmsg ( This, Toplane, msg);} Plane.prototype.receive=function(Fromplane, msg) {Console.log ( This. Name + "-Received from-" + Fromplane.name + "message:" +msg);} //Aircraft Control tower (intermediary)varControltower = (function(){ //Assuming there is only one runway, the runway can only take off one plane (not say landing) varOntrackplanename, Cantrackuse=true; varTakeoff =function(plane) {if(!cantrackuse) {Console.log ("The runway is in use ..."); return; } if(Ontrackplanename = =plane.name) {Console.log ("You are taking off ..."); return; } cantrackuse=false; Ontrackplanename=Plane.name; Console.log (Plane.name+ "Taking off ..."); SetTimeout (function() {Cantrackuse=true; Ontrackplanename=NULL; Console.log (Plane.name+ "Taken off ..."); }, 5000); } varsendmsg =function(from, to, msg) {to.receive (from, msg); } return{takeoff:takeoff, sendmsg:sendmsg}}) (); varp747 =NewPlane (' Boeing 747 ');varp666 =NewPlane (' Flying Leopard 666 '); P747.takeoff ();p 666.takeOff ();p 747.sendMsg (p666,' Have a meal after the plane? '
Applicable scenarios
1. A well-defined set of objects that are now complex to communicate.
2. Customize a behavior that is distributed across multiple classes without having to generate too many subclasses.
As can be seen, the intermediary object is mainly used to encapsulate the behavior of the actors are those objects, but through the intermediary, these objects do not know each other. (Concrete implementation of the Dimitri Law)
Advantages
1. The broker mode reduces the number of communication channels required between objects or components in the system from many-to-many to many-to-one.
2. Complies with the Dimitri principle.
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.
Disadvantages
1. The disadvantage of the intermediary model is obvious, because this "intermediary" assumes more responsibility, so once the intermediary object has a problem, then the whole system will be greatly affected.
The difference between the mediator pattern and the observer pattern;
1. Dispatch center is different.
The mediator mode is dispatched by the mediator, while the observer pattern is the observer;
The broker mode is more like the dispatch mode of the subscription/release pattern, and it is a centralized dispatch center independently.
2. The broker mode participates in the implementation of the object's behavior (for example, the aircraft takeoff above), while the Observer mode does not participate, only the target notification.
Differences between broker mode and subscription/release mode
1. The broker mode is business-related, and the subscription/release model is business agnostic. (This is the biggest difference I feel)
Although the implementation structure is very similar, it is clear that the intermediary model participates in business-related things, so the content is very different.
2. All two modes have a centralized scheduling effect, and the objects are not directly involved in communication.
Reference documents
1. "Learning JavaScript Design Patterns" by Addy Osmani
https://addyosmani.com/resources/essentialjsdesignpatterns/book/
2. "JavaScript design mode" by Xutao "translation"
This article for the original article, reproduced please retain the original source, convenient traceability, if there is the wrong place, thank you correct.
This address: http://www.cnblogs.com/lovesong/p/5575594.html
Design pattern (v): Mediator mode