对于一个模块或者系统,可能由很多对象构成,而且这些对象之间可能存在相互的引用,在最坏的情况下,每一个对象都知道其他所有的对象,这无疑复杂化了对象之间的联系。虽然将一个系统分割成许多对象通常可以增强可复用性,但是对象间相互连接的激增又会降低其可复用性,大量的相互连接使得一个对象似乎不太可能在没有其他对象的支持下工作,系统表现为一个不可分割的整体,而且对系统的行为进行任何较大的改动都会十分困难。结果是你不得不定义大量的子类以定制系统的行为。因此,为了减少对象两两之间复杂的引用关系,使之成为一个松耦合的系统,我们需要使用中介者模式.
中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。中介者模式又称为调停者模式。中介者模式化多对多依赖为一对多依赖。
源代码:
package main const MaxClassSize = 50 //课程最大人数 //中介者接口 type Mediator interface { Register(Mediatable) // 将被协调者的对象保存引用 Mediate(string, ...interface{}) //协调不同对象的操作 } //协同接口 type Mediatable interface { SetMediator(Mediator) } //实现协同接口的简单类,用于子类继承 type SimpleMediatable struct { mediator Mediator } func (s *SimpleMediatable) SetMediator(m Mediator) { s.mediator = m } //具体中介者 type ConcreateMediator struct { coureses map[string]*Course teachers map[string]*Teacher students map[string]*Student courseSelect map[string]([]*Student) } func NewConcreateMediator() *ConcreateMediator { return &ConcreateMediator{make(map[string]*Course), make(map[string]*Teacher), make(map[string]*Student), make(map[string]([]*Student))} } func (c *ConcreateMediator) Register(m Mediatable) { switch m.(type) { case *Course: cname := m.(*Course).Name c.coureses[cname] = m.(*Course) c.courseSelect[cname] = make([]*Student, MaxClassSize) case *Student: c.students[m.(*Student).Name] = m.(*Student) case *Teacher: c.teachers[m.(*Teacher).Name] = m.(*Teacher) } } func (c *ConcreateMediator) Mediate(t string, v ...interface{}) { switch t { case "teach": if courseName, ok := v[0].(string); ok { for num, i, stds := c.coureses[courseName].StdNum, 0, c.courseSelect[courseName]; i < num; i++ { stds[i].listening() println(courseName) } } case "select": if std, ok := v[0].(*Student); ok { if courseName, ok := v[1].(string); ok { pos := &c.coureses[courseName].StdNum c.courseSelect[courseName][*pos] = std *pos++ } } } } type Student struct { SimpleMediatable Name string } func NewStudent(name string, m Mediator) *Student { s := new(Student) s.Name = name s.SetMediator(m) m.Register(s) return s } func (s *Student) selectCourse(cname string) { s.mediator.Mediate("select", s, cname) } func (s *Student) listening() { print(s.Name + " is listening ") } type Course struct { SimpleMediatable Name string StdNum int TeacherName string } func NewCourse(name, tname string, m Mediator) *Course { c := new(Course) c.Name, c.TeacherName = name, tname c.SetMediator(m) m.Register(c) return c } type Teacher struct { SimpleMediatable Name string } func NewTeacher(name string, m Mediator) *Teacher { s := new(Teacher) s.Name = name s.SetMediator(m) m.Register(s) return s } func (t *Teacher) teachCourse(cname string) { t.mediator.Mediate("teach", cname) } func main() { mediatorA := NewConcreateMediator() NewCourse("Math", "Pen TieZhao", mediatorA) NewCourse("Computer", "Liu ZhiRong", mediatorA) std1 := NewStudent("LiChao", mediatorA) std2 := NewStudent("Readen", mediatorA) std3 := NewStudent("Herry", mediatorA) std4 := NewStudent("John", mediatorA) std5 := NewStudent("Richer", mediatorA) std6 := NewStudent("Barry", mediatorA) tch1 := NewTeacher("Pen TieZhao", mediatorA) tch2 := NewTeacher("Liu ZhiRong", mediatorA) std1.selectCourse("Math") std1.selectCourse("Computer") std2.selectCourse("Computer") std3.selectCourse("Math") std4.selectCourse("Math") std4.selectCourse("Computer") std5.selectCourse("Computer") std6.selectCourse("Computer") tch1.teachCourse("Math") tch2.teachCourse("Computer") }
运行结果:
`go run Mediator.go` | done: 1.71875s ] LiChao is listening Math Herry is listening Math John is listening Math LiChao is listening Computer Readen is listening Computer John is listening Computer Richer is listening Computer Barry is listening Computer
有疑问加站长微信联系(非本文作者)