对于一个模块,可能由很多对象构成,而且这些对象之间可能存在相互的引用,为了减少对象两两之间复杂的引用关系,使之称为一个松耦合的系统,这就是中介者模式的模式动机


联合国案例

package other
import "fmt"
//实现Mediator以及ConcreteMediator类
type UnitedNations interface {
   ForwardMessage(message string, country Country)
}
type UnitedNationsSecurityCouncil struct {
   USA
   Iraq
}
func (unsc UnitedNationsSecurityCouncil) ForwardMessage(message string, country Country) {
   switch country.(type) {
   case USA:
      unsc.Iraq.GetMessage(message)
   case Iraq:
      unsc.USA.GetMessage(message)
   default:
      fmt.Println("The country is not a member of UNSC")
   }
}
//实现Colleague以及ConcreteColleague类
type Country interface {
   SendMessage(message string)
   GetMessage(message string)
}
type USA struct {
   UnitedNations
}
func (usa USA) SendMessage(message string) {
   usa.UnitedNations.ForwardMessage(message, usa)
}
func (usa USA) GetMessage(message string) {
   fmt.Printf("美国收到对方消息: %s\n", message)
}
type Iraq struct {
   UnitedNations
}
func (iraq Iraq) SendMessage(message string) {
   iraq.UnitedNations.ForwardMessage(message, iraq)
}
func (iraq Iraq) GetMessage(message string) {
   fmt.Printf("伊拉克收到对方消息: %s\n", message)
}
func TTmain() {
   //创建一个具体中介者
   tMediator := &UnitedNationsSecurityCouncil{}
   //创建具体同事,并且让他认识中介者
   tColleageA := USA{
      UnitedNations: tMediator,
   }
   tColleageB := Iraq{
      UnitedNations: tMediator,
   }
   //让中介者认识每一个具体同事
   tMediator.USA = tColleageA
   tMediator.Iraq = tColleageB
   //A同事发送消息
   tColleageA.SendMessage("停止核武器研发,否则发动战争")
   tColleageB.SendMessage("我们没有研发核武器,也不怕战争")
}
# 测试ttmain()输出
伊拉克收到对方消息: 停止核武器研发,否则发动战争
美国收到对方消息: 我们没有研发核武器,也不怕战争
原文:https://www.cnblogs.com/maomaomaoge/p/14118814.html