{"action":"opened","issue":{"url":"https://api.github.com/repos/archerU/notes/issues/14","repository_url":"https://api.github.com/repos/archerU/notes","labels_url":"https://api.github.com/repos/archerU/notes/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/archerU/notes/issues/14/comments","events_url":"https://api.github.com/repos/archerU/notes/issues/14/events","html_url":"https://github.com/archerU/notes/issues/14","id":329378900,"node_id":"MDU6SXNzdWUzMjkzNzg5MDA=","number":14,"title":"中介者模式","user":{"login":"archerU","id":13237958,"node_id":"MDQ6VXNlcjEzMjM3OTU4","avatar_url":"https://avatars2.githubusercontent.com/u/13237958?v=4","gravatar_id":"","url":"https://api.github.com/users/archerU","html_url":"https://github.com/archerU","followers_url":"https://api.github.com/users/archerU/followers","following_url":"https://api.github.com/users/archerU/following{/other_user}","gists_url":"https://api.github.com/users/archerU/gists{/gist_id}","starred_url":"https://api.github.com/users/archerU/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archerU/subscriptions","organizations_url":"https://api.github.com/users/archerU/orgs","repos_url":"https://api.github.com/users/archerU/repos","events_url":"https://api.github.com/users/archerU/events{/privacy}","received_events_url":"https://api.github.com/users/archerU/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-05T09:42:40Z","updated_at":"2018-06-05T09:42:40Z","closed_at":null,"author_association":"OWNER","body":"\r\n\r\n中介者模式的作用就是解除对象与对象之间的紧耦合关系。\r\n\r\n中介者模式是迎合迪米特原则（最小知识原则：一个对象应该尽可能少地了解另外一个对象）的一种实现。\r\n\r\n中介者模式优点：1、对象之间解耦，以中介者和对象的一对多关系取代对象之间的网状多对多关系。对象之间的交互关系由中介者维护，对象关注自身功能。\r\n\r\n中介者模式缺点：1、对象之间的复杂关系转移到中介者对象，中介者对象本身往往就是一个难以维护的对象。2、中介者对象消耗内存。\r\n\r\n## 中介者模式例子\r\n\r\n```\r\nfunction Player(name,teamColor) {\r\n  this.name = name; // \r\n  this.teamColor = teamColor;\r\n  this.state = 'alive';\r\n}\r\nPlayer.prototype.win = function() {\r\n\tconsole.log(this.name + 'win');\r\n}\r\nPlayer.prototype.lose = function() {\r\n\tconsole.log(this.name + 'lost');\r\n}\r\nPlayer.prototype.die = function() {\r\n\tthis.state = 'dead';\r\n\tplayerDirector.ReceiveMessage('playerDead',this);\r\n}\r\nPlayer.prototype.remove = function() {\r\n\tplayerDirector.ReceiveMessage('removePlayer',this);\r\n}\r\nPlayer.prototype.changeTeam = function() {\r\n\tplayerDirector.ReceiveMessage('changeTeam',this,color);\r\n}\r\n\r\nfunction playerFactory(name,teamColor) {\r\n\tconst newPlayer = new Player(name,teamColor);\r\n\tplayerDirector.ReceiveMessage('addPlayer',newPlayer);\r\n\treturn newPlayer;\r\n}\r\n\r\nconst playerDirector = (function(){\r\n\tconst players = {},operations = {};\r\n\r\n\toperations.addPlayer = function(player) {\r\n\t\tconst teamColor = player.teamColor;\r\n\t\tplayers[teamColor] = players[teamColor] || [];\r\n\t\tplayers[teamColor].push(player);\r\n\t}\r\n\r\n\toperations.removePlayer = function(player) {\r\n\t\tconst teamColor = player.teamColor;\r\n\t\tconst teamPalyers = players[teamColor] || [];\r\n\t\tfor(let i=teamPalyers.length-1;i>=0;i--) {\r\n\t\t\tif (teamPalyers[i] === player) {\r\n\t\t\t\tteamPalyers.splice(i,1);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\toperations.changeTeam = function(player,newTeamColor) {\r\n\t\toperations.removePlayer(player);\r\n\t\tplayer.teamColor = newTeamColor;\r\n\t\toperations.addPlayer(player);\r\n\t}\r\n\r\n\toperations.playerDead = function(player) {\r\n\t\tconst teamColor = player.teamColor,\r\n\t\tteamPalyers = players[teamColor];\r\n\r\n\t\tlet all_dead = true;\r\n\t\tfor(let i =0,length = teamPalyers.length;i<length ;i++) {\r\n\t\t\tif (player.state !== 'dead') {\r\n\t\t\t\tall_dead = false;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (all_dead) {\r\n\t\t\tfor (let i=0,length=teamPalyers.length;i<length;i++) {\r\n\t\t\t\tplayer.lose();\r\n\t\t\t}\r\n\t\t\tfor (let color in players) {\r\n\t\t\t\tif (color !== teamColor) {\r\n\t\t\t\t\tconst teamPalyers = players[color];\r\n\t\t\t\t\tfor (let i=0,length=teamPalyers.length;i<length;i++) {\r\n\t\t\t\t\t\tlet player = teamPalyers[i];\r\n\t\t\t\t\t\tplayer.win();\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tconst ReceiveMessage = function() {\r\n\t\tconst message = Array.prototype.shift.call(arguments);\r\n\t\toperations[message].apply(this,arguments);\r\n\t}\r\n\treturn {\r\n\t\tReceiveMessage\r\n\t}\r\n})();\r\n\r\n\r\nconst player1 = playerFactory('皮蛋','red');\r\nconst player5 = playerFactory('黑妞','blue');\r\n\r\nplayer1.die();\r\n```\r\n\r\n"}}
{"actor":{"display_login":"archerU"}}