Mediator Mode Go language implementation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

For a module or system, it may consist of many objects, and there may be mutual references between these objects, and in the worst case, every object knows all other objects, which complicates the connection between the objects. Although splitting a system into many objects can often enhance reusability, the proliferation of inter-object connections reduces its reusability, and a large number of interconnected objects seem unlikely to work without the support of other objects, and the system behaves as an indivisible whole. And any major changes to the system's behavior can be very difficult. The result is that you have to define a large number of subclasses to customize the behavior of the system. Therefore, in order to reduce the complex reference relationship between object 22 and make it a loosely coupled system, we need to use the mediator pattern.

Mediator mode: A Mediation object is used to encapsulate a series of object interactions. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them. The mediator pattern is also known as the mediator pattern. Intermediaries are modeled on many-to-many dependencies for a one-to-many dependency .

Source:

Package Mainconst maxclasssize = 50//course maximum//Broker interface type Mediator interface {Register (mediatable)//will be the object of the coordinator to save the citation Use mediate (String, ... interface{})//coordinate operation of different objects}//cooperative interface type Mediatable interface {setmediator (mediator)}//a simple class that implements a synergistic interface, For subclass inheritance type simplemediatable struct {mediator Mediator}func (s *simplemediatable) setmediator (M mediator) {s.mediator = m} Specific mediator type concreatemediator struct {coureses map[string]*courseteachers map[string]*teacherstudents Map[stri Ng]*studentcourseselect 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). Namec.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 Coursenam E, 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]. Stdnumc.courseselect[coursename][*pos] = Std*pos++}}}}type Student struct {simplemediatablename string}func Newstudent (name string, m mediator) *student {s: = new (Student) S.name = names. 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 {simplemediatablename str Ingstdnum intteachername string}func newcourse (name, tname string, M mediator) *course {c: = new (Course) C.name, C.tea Chername = name, Tnamec. Setmediator (M) m.Register (c) return c}type Teacher struct {simplemediatablename string}func newteacher (name string, m mediator) *teacher {s : = new (Teacher) S.name = names. 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")}

Operation Result:


' Go run mediator.go ' |  Done:1.71875s]lichao is listening mathherry are listening Mathjohn is listening Mathlichao are listening Computerreaden is Listening Computerjohn is listening computerricher are listening Computerbarry is listening computer


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.