中介者模式:用一个中介对象来封装一系列对象的交互,从而把一批原来可能是交互关系复杂的对象转换成一组松散耦合的中间对象,以有利于维护和修改
中介者模式适用场景:
- 系统中对象间存在较为复杂引用,导致依赖关系和结构混乱而无法复用的情况。
- 想通过一个中间类来封装多个类的行为,但是又不想要太多的子类。
中介者模式组件:
- Mediator(抽象中介者):定义了各个同事之间交互需要的方法
- ConcreteMediator(具体中介者):需要了解维护各个同事对象,并且负责协调各个具体同事之间的交互
- Colleague(抽象同事角色):约束具体同事类的类型、并且实现一些具体同事类之间的公共方法
- ConcreteColleague(具体同事角色):每个同事都知道中介者对象,要与同事通信则把通信告诉中介者
中介者模式组件实例:
抽象中介者:定义了各个同事角色之间交互需要的方法 act(PartyMember actor, Action action)
public interface Party {
void addMember(PartyMember member);
void act(PartyMember actor, Action action);
}
具体中介者:
import java.util.ArrayList;
import java.util.List;
public class PartyImpl implements Party {
private final List<PartyMember> members;
public PartyImpl() {
members = new ArrayList<>();
}
@Override
public void act(PartyMember actor, Action action) {
for (PartyMember member : members) {
if (!member.equals(actor)) {
member.partyAction(action);
}
}
}
@Override
public void addMember(PartyMember member) {
members.add(member);
member.joinedParty(this);
}
}
抽象同事角色:
public interface PartyMember {
void joinedParty(Party party);
void partyAction(Action action);
void act(Action action);
}
具体同事角色:
public abstract class PartyMemberBase implements PartyMember {
protected Party party;
@Override
public void joinedParty(Party party) {
this.party = party;
}
@Override
public void partyAction(Action action) {
System.out.println(action.getDescription());
}
@Override
public void act(Action action) {
if (party != null) {
System.out.println(action);
party.act(this, action);
}
}
@Override
public abstract String toString();
}
public class Hobbit extends PartyMemberBase {
@Override
public String toString() {
return "Hobbit";
}
}
public class Hunter extends PartyMemberBase {
@Override
public String toString() {
return "Hunter";
}
}
public class Rogue extends PartyMemberBase {
@Override
public String toString() {
return "Rogue";
}
}
public class Wizard extends PartyMemberBase {
@Override
public String toString() {
return "Wizard";
}
}
public enum Action {
HUNT("hunted a rabbit", "arrives for dinner"),
TALE("tells a tale", "comes to listen"),
GOLD("found gold", "takes his share of the gold"),
ENEMY("spotted enemies", "runs for cover"),
NONE("", "");
private final String title;
private final String description;
Action(String title, String description) {
this.title = title;
this.description = description;
}
public String getDescription() {
return description;
}
public String toString() {
return title;
}
}
客户端调用:
public class App {
public static void main(String[] args) {
Party party = new PartyImpl();
PartyMember hobbit = new Hobbit();
PartyMember wizard = new Wizard();
PartyMember rogue = new Rogue();
PartyMember hunter = new Hunter();
party.addMember(hobbit);
party.addMember(wizard);
party.addMember(rogue);
party.addMember(hunter);
hobbit.act(Action.ENEMY);
wizard.act(Action.TALE);
rogue.act(Action.GOLD);
hunter.act(Action.HUNT);
}
}
中介者模式总结:
- 将多个对象之间的联系紧耦合封装到中介对象中,做到松耦合。
- 将多个对象之间的交互联系集中在中介对象中。发送变化仅需修改中介对象即可、提供系统的灵活性、使同事对象独立而易于复用。
- 符合迪米特原则。
- 如果各个同事间的交互非常多并且复杂,会导致中介者也变得十分复杂,不易维护和管理。
来源:oschina
链接:https://my.oschina.net/u/3057088/blog/4815468