golang与java 中介者模式

tech2023-02-18  112

1.定义

定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。

2.理解

就像在现实中一样,我们想要和身边的人通信,我们需要知道他的联系方式,直接和他通信。当系统中,对象比较多时,这种方式就非常不方便了。通过中介者,我们只需要将要传达的信息交给中介者就好,中介者会将该信息传递给所有注册到中介者的对象上。将原本多对多的关系转化成了多对一。所有的对象只需要关心中介者的存在即可。

3.结构

中介者模式包含以下主要角色。

抽象中介者(Mediator)角色:它是中介者的接口,提供了同事对象注册与转发同事对象信息的抽象方法。具体中介者(ConcreteMediator)角色:实现中介者接口,定义一个 List 来管理同事对象,协调各个同事角色之间的交互关系,因此它依赖于同事角色。抽象同事类(Colleague)角色:定义同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能。具体同事类(Concrete Colleague)角色:是抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互。

4.java示例

public class MediatorPattern { public static void main(String[] args) { //中介 IMediator mediator = new ConcreteMediator(); //同事 ICollegue collegue1 = new ConcreteCollegue("同事1"); ICollegue collegue2 = new ConcreteCollegue("同事2"); ICollegue collegue3 = new ConcreteCollegue("同事3"); ICollegue collegue4 = new ConcreteCollegue("同事4"); //不用中介者模式的话,同事1要想和其它同事通信只能通过 collegue2.receive("同事1想传达的信息"); collegue3.receive("同事1想传达的信息"); collegue4.receive("同事1想传达的信息"); //即直接通过拥有其它同事的引用,调用其接收信息的方法。当同事数量很大时,很不方便,而且,在现实生活中,我们不一定拥有它们的联系方式(引用) //注册到中介者上, //也可以通过:mediator.register()方法 collegue1.setMediator(mediator); collegue2.setMediator(mediator); collegue3.setMediator(mediator); collegue4.setMediator(mediator); //现在通过中介者来进行通信 //也可以通过这方法:mediator.relay(collegue1,"同事1想要的传达的信息"); collegue1.send("同事1想要传达的信息"); } } //抽象中介 interface IMediator{ void register(ICollegue collegue); void relay(ICollegue collegue,String message); } //具体中介 class ConcreteMediator implements IMediator{ Set<ICollegue> collegues = new HashSet<>(); @Override public void register(ICollegue collegue) { if (!collegues.contains(collegue)){ collegues.add(collegue); } } @Override public void relay(ICollegue collegue,String message) { collegues.forEach(e->{ if (e != collegue){ e.receive(message); } }); } } //抽象同事类 interface ICollegue{ void setMediator(IMediator mediator); void receive(String message); void send(String message); } //具体同事类 class ConcreteCollegue implements ICollegue{ public String Name; public ConcreteCollegue(String name){ this.Name = name; } private IMediator mediator; @Override public void setMediator(IMediator mediator) { this.mediator = mediator; mediator.register(this); } @Override public void receive(String message) { System.out.println("我是"+this.Name+"收到了消息:"+message); } @Override public void send(String message) { this.mediator.relay(this,message); } }

5.golang示例

func main() { //中介者 mediator := NewConcreteMediator(); //具体同事 collegue1 := &ConcreteCollegue{ Name: "同事1", Mediator: mediator, } collegue2 := &ConcreteCollegue{ Name: "同事2", Mediator: mediator, } collegue3 := &ConcreteCollegue{ Name: "同事3", Mediator: mediator, } collegue4 := &ConcreteCollegue{ Name: "同事4", Mediator: mediator, } //注册到中介者上 mediator.Register(collegue1) mediator.Register(collegue2) mediator.Register(collegue3) mediator.Register(collegue4) //同事1向其它同事发消息 mediator.Relay(collegue1,"同事1想要传达的信息") } //抽象同事类 type ICollegue interface { SetMediator(mediator IMediator) Receive(message string) Send(message string) } //抽象中介类 type IMediator interface { Register(collegue ICollegue) Relay(collegue ICollegue,message string) } //具体同事类 type ConcreteCollegue struct { Name string Mediator IMediator } func (collegue *ConcreteCollegue) Receive(message string) { fmt.Println("我是",collegue.Name,"收到了消息:",message) } func (collegue *ConcreteCollegue) Send(message string) { collegue.Mediator.Relay(collegue,message) } func (collegue *ConcreteCollegue) SetMediator(mediator IMediator) { collegue.Mediator = mediator mediator.Register(collegue) } //具体中介类 type ConcreteMediator struct { Collegues map[ICollegue]Empty//此处仅仅是把map当做set来用 } //一个空的结构体 type Empty struct { } func (mediator *ConcreteMediator) Register(collegue ICollegue) { mediator.Collegues[collegue] = Empty{} } func (mediator *ConcreteMediator) Relay(collegue ICollegue, message string) { for k,_ := range mediator.Collegues{ if k != collegue{ k.Receive(message) } } } //工厂方法模式 func NewConcreteMediator()IMediator{ mediator := new(ConcreteMediator) mediator.Collegues = make(map[ICollegue]Empty) return mediator }

最新回复(0)