What is the broker mode?
Mediator mode: A third-party mediator communicates between objects and objects.
definition: Using a Mediation object to encapsulate a series of object interactions, the mediator makes the objects do not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them.
The main solution: There is a large number of relations between objects and objects, which will inevitably lead to the structure of the system becomes very complex, and if an object changes, we also need to track the object associated with it, at the same time to make corresponding processing.
when to use: Multiple classes are coupled to each other, forming a network structure.
How to solve: The above-mentioned network structure is separated into a star structure.
Application Example: 1, before China's accession to the WTO is the various countries trade with each other, the structure is complex, now is each country through the WTO to each other trade. 2, airport dispatching system. 3. The MVC framework, where C (Controller) is the mediator of M (model) and V (view).
Advantages: 1, reduce the complexity of the class, one-to-many conversion into one-to-one. 2, the decoupling between the various classes. 3, in line with the Dimitri principle.
Cons: intermediaries can be huge and complex and difficult to maintain.
usage Scenario: 1. There are more complex referential relationships between objects in the system, resulting in a confusing structure of dependencies between them and the difficulty of reusing the object. 2, want to encapsulate the behavior of multiple classes through an intermediate class, but do not want to generate too many subclasses.
Note: It should not be used in the event of confusion of responsibilities.
Scenario Demo
After a test, announce the result: Tell the person who answered the question to challenge the success, or the challenge will fail.
Const PLAYER =function(name) { This. Name =name Playermiddle.add (name)}player.prototype.win=function() {Playermiddle.win ( This. Name)} Player.prototype.lose=function() {Playermiddle.lose ( This. Name)} Const Playermiddle= (function() {//will use this demo, this function as a mediatorConst PLAYERS =[] Const Winarr=[] Const Losearr= [] return{add:function(name) {Players.push (name)}, win:function(name) {Winarr.push (name)if(winarr.length + losearr.length = = =players.length) { This. Show ()}}, Lose:function(name) {Losearr.push (name)if(winarr.length + losearr.length = = =players.length) { This. Show ()}}, show:function() { for(Let winner of Winarr) {Console.log (winner+ ' challenge success; ') } for(let loser of Losearr) {Console.log (loser+ ' challenge failed; ')}},}} ()) const a=NewPlayer (' A player ') Const B=NewPlayer (' B player ') Const C=NewPlayer (' C player ') A.win () B.win () C.lose ( )//A Player challenges success;//B Player Challenge success;//C player challenge failed;
There is no direct relationship between A, B, and C in this code, but instead of creating a link through another Playermiddle object, consider it as a mediator pattern.
JS design mode (11) Broker mode