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");
collegue2
.receive("同事1想传达的信息");
collegue3
.receive("同事1想传达的信息");
collegue4
.receive("同事1想传达的信息");
collegue1
.setMediator(mediator
);
collegue2
.setMediator(mediator
);
collegue3
.setMediator(mediator
);
collegue4
.setMediator(mediator
);
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
)
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
}
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
}