中介者模式
中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。例如,在 MVC 框架中,控制器(C)就是模型(M)和视图(V)的中介者;
意图:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
主要解决:对象与对象之间存在大量的关联关系,这样势必会导致系统的结构变得很复杂,同时若一个对象发生改变,我们也需要跟踪与之相关联的对象,同时做出相应的处理。
何时使用:多个类相互耦合,形成了网状结构。
如何解决:将上述网状结构分离为星型结构。
关键代码:对象 Colleague 之间的通信封装到一个类中单独处理。
优点: 1、降低了类的复杂度,将一对多转化成了一对一。 2、各个类之间的解耦。 3、符合迪米特原则。
缺点:中介者会庞大,变得复杂难以维护。
使用场景: 1、系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。 2、想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。
注意事项:不应当在职责混乱的时候使用。
中介者模式包含以下主要角色:
1.抽象中介者(Mediator)角色:它是中介者的接口,提供了同事对象注册与转发同事对象信息的抽象方法。
2.具体中介者(ConcreteMediator)角色:实现中介者接口,定义一个 List 来管理同事对象,协调各个同事角色之间的交互关系,因此它依赖于同事角色。
3.抽象同事类(Colleague)角色:定义同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能。
4.具体同事类(Concrete Colleague)角色:是抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互。
/**
*
* @ClassName: Mediator
* @Description: 抽象中介类
* @author: ljx
* @date: 2019年3月18日 下午3:00:13
*/
public abstract class Mediator {
abstract void register(Colleague colleague);//注册
abstract void relay(Colleague colleague,String msg);//转发
}
/**
*
* @ClassName: ConcreteMediator
* @Description: 具体中介类
* @author: ljx
* @date: 2019年3月18日 下午4:09:18
*/
public class ConcreteMediator extends Mediator {
//创建一个list来管理同事类
private List<Colleague> list = new ArrayList<Colleague>();
@Override
void register(Colleague colleague) {
//list是否包含colleague
if(!list.contains(colleague)){
//添加并设置中介类
list.add(colleague);
colleague.setMedium(this);
}
}
@Override
void relay(Colleague colleague,String msg) {
//遍历客户
for (Colleague cl : list) {
//如果不为发送者,则进行接收
if(!cl.equals(colleague)){
cl.recive(msg);
}
}
}
}
/**
*
* @ClassName: Colleague
* @Description: 抽象同事类
* @author: ljx
* @date: 2019年3月18日 下午2:59:01
*/
public abstract class Colleague {
protected Mediator mediator;
public void setMedium(Mediator mediator){
this.mediator = mediator;
}
public abstract void recive(String msg);//接收
public abstract void send(String msg);//发送
}
/**
*
* @ClassName: ConcreteColleague1
* @Description: 具体同事类1
* @author: ljx
* @date: 2019年3月18日 下午4:09:02
*/
public class ConcreteColleague1 extends Colleague {
@Override
public void recive(String msg) {
System.out.println("具体同事类1收到请求:"+msg);
}
@Override
public void send(String msg) {
System.out.println("具体同事类1发出请求:"+msg);
mediator.relay(this,msg);//请中介者转发
}
}
/**
*
* @ClassName: ConcreteColleague2
* @Description: 具体同事类2
* @author: ljx
* @date: 2019年3月18日 下午4:08:45
*/
public class ConcreteColleague2 extends Colleague {
@Override
public void recive(String msg) {
System.out.println("具体同事类2收到请求:"+msg);
}
@Override
public void send(String msg) {
System.out.println("具体同事类2发出请求:"+msg);
mediator.relay(this,msg);//请中介者转发
}
}
public class MediatorTest {
public static void main(String[] args) {
//创建中介类
Mediator mediator = new ConcreteMediator();
//创建客户
Colleague colleague1 = new ConcreteColleague1();
Colleague colleague2 = new ConcreteColleague1();
//注册
mediator.register(colleague1);
mediator.register(colleague2);
colleague1.send("你好!");
System.out.println("----------------------");
colleague2.send("我很好!");
}
}
在实际开发中,通常采用以下两种方法来简化中介者模式,使开发变得更简单。
1.不定义中介者接口,把具体中介者对象实现成为单例。
2.同事对象不持有中介者,而是在需要的时直接获取中介者对象并调用。
如图所示是简化中介者模式的结构图:
import java.util.ArrayList;
import java.util.List;
/**
*
* @ClassName: SimpleMediator
* @Description: 简单中介者模式
* @author: ljx
* @date: 2019年3月18日 下午4:26:02
*/
public class SimpleMediator {
private static SimpleMediator smd = new SimpleMediator();
private List<SimpleColleague> colleagues=new ArrayList<SimpleColleague>();
//私有自身
private SimpleMediator(){}
//获取自身
public static SimpleMediator getMedium(){
return smd;
}
//注册
public void register(SimpleColleague colleague){
//如果不包含则添加
if(!colleagues.contains(colleague)){
colleagues.add(colleague);
}
}
//转发
public void relay(SimpleColleague colleague){
for (SimpleColleague simpleColleague : colleagues) {
if(!colleague.equals(simpleColleague)){
simpleColleague.receive();
}
}
}
}
/**
*
* @ClassName: SimpleColleague
* @Description: 抽象同事类
* @author: ljx
* @date: 2019年3月18日 下午4:27:06
*/
public interface SimpleColleague {
void receive();
void send();
}
/**
*
* @ClassName: SimpleConcreteColleague1
* @Description: 具体同事类1
* @author: ljx
* @date: 2019年3月18日 下午4:33:53
*/
public class SimpleConcreteColleague1 implements SimpleColleague {
@Override
public void receive() {
System.out.println("同事1接收信息");
}
@Override
public void send() {
//需要才使用
SimpleMediator smd = SimpleMediator.getMedium();
System.out.println("同事1发送信息");
smd.relay(this); //请中介者转发
}
}
/**
*
* @ClassName: SimpleConcreteColleague2
* @Description: 具体同事类2
* @author: ljx
* @date: 2019年3月18日 下午4:40:32
*/
public class SimpleConcreteColleague2 implements SimpleColleague {
@Override
public void receive() {
System.out.println("同事2接收信息");
}
@Override
public void send() {
//需要才使用
SimpleMediator smd = SimpleMediator.getMedium();
System.out.println("同事2发送信息");
smd.relay(this); //请中介者转发
}
}
public class SimpleMediatorTest {
public static void main(String[] args) {
SimpleColleague simpleColleague1 = new SimpleConcreteColleague1();
SimpleColleague simpleColleague2 = new SimpleConcreteColleague2();
simpleColleague1.send();
simpleColleague2.send();
}
}
... ...,欢迎指正!