added mediator sample

This commit is contained in:
Ilkka Seppala 2014-08-21 20:06:49 +03:00
parent 46408565cd
commit 82f5f88964
12 changed files with 217 additions and 0 deletions

23
mediator/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<artifactId>mediator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mediator</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,28 @@
package com.iluwatar;
public enum Action {
HUNT, TALE, GOLD, ENEMY;
public String toString() {
String s = "";
switch (this) {
case ENEMY:
s = "spotted enemies";
break;
case GOLD:
s = "found gold";
break;
case HUNT:
s = "hunted a rabbit";
break;
case TALE:
s = "tells a tale";
break;
default:
break;
}
return s;
};
}

View File

@ -0,0 +1,23 @@
package com.iluwatar;
public class App
{
public static void main( String[] args )
{
Party party = new PartyImpl();
Hobbit hobbit = new Hobbit();
Wizard wizard = new Wizard();
Rogue rogue = new Rogue();
Hunter 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);
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class Hobbit extends PartyMemberBase {
@Override
public String toString() {
return "Hobbit";
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class Hunter extends PartyMemberBase {
@Override
public String toString() {
return "Hunter";
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public interface Party {
void addMember(PartyMember member);
void act(PartyMember actor, Action action);
}

View File

@ -0,0 +1,37 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.List;
public class PartyImpl implements Party {
private List<PartyMember> members;
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 addMember(PartyMember member) {
members.add(member);
member.joinedParty(this);
}
// somebody hunts for food, call for dinner
// somebody spots enemy, alert everybody
// somebody finds gold, deal the gold with everybody
// somebody tells a tale, call everybody to listen
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public interface PartyMember {
void joinedParty(Party party);
void partyAction(Action action);
void act(Action action);
}

View File

@ -0,0 +1,46 @@
package com.iluwatar;
public abstract class PartyMemberBase implements PartyMember {
protected Party party;
@Override
public void joinedParty(Party party) {
System.out.println(this + " joins the party");
this.party = party;
}
@Override
public void partyAction(Action action) {
String s = this + " ";
switch (action) {
case ENEMY:
s = s + "runs for cover";
break;
case GOLD:
s = s + "takes his share of the gold";
break;
case HUNT:
s = s + "arrives for dinner";
break;
case TALE:
s = s + "comes to listen";
break;
default:
break;
}
System.out.println(s);
}
@Override
public void act(Action action) {
if (party != null) {
System.out.println(this + " " + action.toString());
party.act(this, action);
}
}
@Override
public abstract String toString();
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class Rogue extends PartyMemberBase {
@Override
public String toString() {
return "Rogue";
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class Wizard extends PartyMemberBase {
@Override
public String toString() {
return "Wizard";
}
}

View File

@ -34,6 +34,7 @@
<module>command</module>
<module>interpreter</module>
<module>iterator</module>
<module>mediator</module>
</modules>
<build>