Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -7,25 +7,23 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
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("", "");
|
||||
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;
|
||||
private String title;
|
||||
private String description;
|
||||
|
||||
Action(String title, String description) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
}
|
||||
Action(String title, String description) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -2,54 +2,53 @@ package com.iluwatar.mediator;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
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();
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// add party members
|
||||
party.addMember(hobbit);
|
||||
party.addMember(wizard);
|
||||
party.addMember(rogue);
|
||||
party.addMember(hunter);
|
||||
// create party and members
|
||||
Party party = new PartyImpl();
|
||||
Hobbit hobbit = new Hobbit();
|
||||
Wizard wizard = new Wizard();
|
||||
Rogue rogue = new Rogue();
|
||||
Hunter hunter = new 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public class Hobbit extends PartyMemberBase {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Hobbit";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Hobbit";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,9 +7,8 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public class Hunter extends PartyMemberBase {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Hunter";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Hunter";
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public interface Party {
|
||||
|
||||
void addMember(PartyMember member);
|
||||
void addMember(PartyMember member);
|
||||
|
||||
void act(PartyMember actor, Action action);
|
||||
void act(PartyMember actor, Action action);
|
||||
|
||||
}
|
||||
|
@ -10,24 +10,24 @@ import java.util.List;
|
||||
*/
|
||||
public class PartyImpl implements Party {
|
||||
|
||||
private final List<PartyMember> members;
|
||||
private final List<PartyMember> members;
|
||||
|
||||
public PartyImpl() {
|
||||
members = new ArrayList<>();
|
||||
}
|
||||
public PartyImpl() {
|
||||
members = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void act(PartyMember actor, Action action) {
|
||||
for (PartyMember member : members) {
|
||||
if (member != actor) {
|
||||
member.partyAction(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void act(PartyMember actor, Action action) {
|
||||
for (PartyMember member : members) {
|
||||
if (member != actor) {
|
||||
member.partyAction(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMember(PartyMember member) {
|
||||
members.add(member);
|
||||
member.joinedParty(this);
|
||||
}
|
||||
@Override
|
||||
public void addMember(PartyMember member) {
|
||||
members.add(member);
|
||||
member.joinedParty(this);
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public interface PartyMember {
|
||||
|
||||
void joinedParty(Party party);
|
||||
void joinedParty(Party party);
|
||||
|
||||
void partyAction(Action action);
|
||||
void partyAction(Action action);
|
||||
|
||||
void act(Action action);
|
||||
void act(Action action);
|
||||
}
|
||||
|
@ -7,28 +7,28 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public abstract class PartyMemberBase implements PartyMember {
|
||||
|
||||
protected Party party;
|
||||
protected Party party;
|
||||
|
||||
@Override
|
||||
public void joinedParty(Party party) {
|
||||
System.out.println(this + " joins the party");
|
||||
this.party = party;
|
||||
}
|
||||
@Override
|
||||
public void joinedParty(Party party) {
|
||||
System.out.println(this + " joins the party");
|
||||
this.party = party;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void partyAction(Action action) {
|
||||
System.out.println(this + " " + action.getDescription());
|
||||
}
|
||||
@Override
|
||||
public void partyAction(Action action) {
|
||||
System.out.println(this + " " + action.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void act(Action action) {
|
||||
if (party != null) {
|
||||
System.out.println(this + " " + action.toString());
|
||||
party.act(this, action);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void act(Action action) {
|
||||
if (party != null) {
|
||||
System.out.println(this + " " + action.toString());
|
||||
party.act(this, action);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public class Rogue extends PartyMemberBase {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Rogue";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Rogue";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ package com.iluwatar.mediator;
|
||||
*/
|
||||
public class Wizard extends PartyMemberBase {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Wizard";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Wizard";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.mediator.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user