中介者模式

中介者模式UML类图

中介者模式UML类图

中介者模式java实现

/**
* Action enumeration.
*/
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 String title;
private String description;

Action(String title, String description) {
this.title = title;
this.description = description;
}

public String getDescription() {
return description;
}

public String toString() {
return title;
}
}

/**
* Party interface.
*/
public interface Party {

void addMember(PartyMember member);

void act(PartyMember actor, Action action);

}

/**
* Interface for party members interacting with {@link Party}.
*/
public interface PartyMember {

void joinedParty(Party party);

void partyAction(Action action);

void act(Action action);
}

/**
* Party implementation.
*/
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);
}
}

/**
* Abstract base class for party members.
*/
public abstract class PartyMemberBase implements PartyMember {

private static final Logger LOGGER = LoggerFactory.getLogger(PartyMemberBase.class);

protected Party party;

@Override
public void joinedParty(Party party) {
LOGGER.info("{} joins the party", this);
this.party = party;
}

@Override
public void partyAction(Action action) {
LOGGER.info("{} {}", this, action.getDescription());
}

@Override
public void act(Action action) {
if (party != null) {
LOGGER.info("{} {}", this, action);
party.act(this, action);
}
}

@Override
public abstract String toString();

}

/**
* Hobbit party member.
*/
public class Hobbit extends PartyMemberBase {

@Override
public String toString() {
return "Hobbit";
}

}

/**
* Hunter party member.
*/
public class Hunter extends PartyMemberBase {

@Override
public String toString() {
return "Hunter";
}
}

/**
* Rogue party member.
*/
public class Rogue extends PartyMemberBase {

@Override
public String toString() {
return "Rogue";
}

}

/**
* Wizard party member.
*/
public class Wizard extends PartyMemberBase {

@Override
public String toString() {
return "Wizard";
}

}

/**
* The Mediator pattern defines an object that encapsulates how a set of objects interact. This
* pattern is considered to be a behavioral pattern due to the way it can alter the program's
* running behavior.
*
* <p>Usually a program is made up of a large number of classes. So the logic and computation is
* distributed among these classes. However, as more classes are developed in a program, especially
* during maintenance and/or refactoring, the problem of communication between these classes may
* become more complex. This makes the program harder to read and maintain. Furthermore, it can
* become difficult to change the program, since any change may affect code in several other
* classes.
*
* <p>With the Mediator pattern, communication between objects is encapsulated with a mediator
* object. Objects no longer communicate directly with each other, but instead communicate through
* the mediator. This reduces the dependencies between communicating objects, thereby lowering the
* coupling.
*
* <p>In this example the mediator encapsulates how a set of objects ({@link PartyMember})
* interact. Instead of referring to each other directly they use the mediator ({@link Party})
* interface.
介体模式定义了一个对象,该对象封装了一组对象之间的交互方式。由于该模式可以更改程序的运行行为,因此该模式被视为行为模式。
<p>通常,一个程序由许多类组成。因此,逻辑和计算分布在这些类之间。但是,随着程序中开发出更多的类,尤其是在维护和/或重构期间,这些类之间的通信问题可能会变得更加复杂。这使得程序难以阅读和维护。此外,更改程序可能变得困难,因为任何更改都可能影响其他几个类中的代码。
<p>在Mediator模式下,对象之间的通信被Mediator对象封装。对象不再彼此直接通信,而是通过调解器进行通信。这减少了通信对象之间的依赖性,从而降低了耦合。
<p>在此示例中,中介者封装了一组对象({@link PartyMember})如何交互。他们使用中介器({@link Party})界面而不是直接相互引用。
*/
public class App {

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {

// create party and members
Party party = new PartyImpl();
Hobbit hobbit = new Hobbit();
Wizard wizard = new Wizard();
Rogue rogue = new Rogue();
Hunter hunter = new Hunter();

// add party members
party.addMember(hobbit);
party.addMember(wizard);
party.addMember(rogue);
party.addMember(hunter);

// perform actions -> the other party members
// are notified by the party
hobbit.act(Action.ENEMY);
wizard.act(Action.TALE);
rogue.act(Action.GOLD);
hunter.act(Action.HUNT);
}
}