File tree Expand file tree Collapse file tree 12 files changed +217
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 12 files changed +217
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" ?>
2+ <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"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
4+ <modelVersion >4.0.0</modelVersion >
5+ <parent >
6+ <groupId >com.iluwatar</groupId >
7+ <artifactId >java-design-patterns</artifactId >
8+ <version >1.0-SNAPSHOT</version >
9+ </parent >
10+ <groupId >com.iluwatar</groupId >
11+ <artifactId >mediator</artifactId >
12+ <version >1.0-SNAPSHOT</version >
13+ <name >mediator</name >
14+ <url >http://maven.apache.org</url >
15+ <dependencies >
16+ <dependency >
17+ <groupId >junit</groupId >
18+ <artifactId >junit</artifactId >
19+ <version >3.8.1</version >
20+ <scope >test</scope >
21+ </dependency >
22+ </dependencies >
23+ </project >
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public enum Action {
4+
5+ HUNT , TALE , GOLD , ENEMY ;
6+
7+ public String toString () {
8+
9+ String s = "" ;
10+ switch (this ) {
11+ case ENEMY :
12+ s = "spotted enemies" ;
13+ break ;
14+ case GOLD :
15+ s = "found gold" ;
16+ break ;
17+ case HUNT :
18+ s = "hunted a rabbit" ;
19+ break ;
20+ case TALE :
21+ s = "tells a tale" ;
22+ break ;
23+ default :
24+ break ;
25+ }
26+ return s ;
27+ };
28+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class App
4+ {
5+ public static void main ( String [] args )
6+ {
7+ Party party = new PartyImpl ();
8+ Hobbit hobbit = new Hobbit ();
9+ Wizard wizard = new Wizard ();
10+ Rogue rogue = new Rogue ();
11+ Hunter hunter = new Hunter ();
12+
13+ party .addMember (hobbit );
14+ party .addMember (wizard );
15+ party .addMember (rogue );
16+ party .addMember (hunter );
17+
18+ hobbit .act (Action .ENEMY );
19+ wizard .act (Action .TALE );
20+ rogue .act (Action .GOLD );
21+ hunter .act (Action .HUNT );
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class Hobbit extends PartyMemberBase {
4+
5+ @ Override
6+ public String toString () {
7+ return "Hobbit" ;
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class Hunter extends PartyMemberBase {
4+
5+ @ Override
6+ public String toString () {
7+ return "Hunter" ;
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface Party {
4+
5+ void addMember (PartyMember member );
6+
7+ void act (PartyMember actor , Action action );
8+
9+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ public class PartyImpl implements Party {
7+
8+ private List <PartyMember > members ;
9+
10+ public PartyImpl () {
11+ members = new ArrayList <>();
12+ }
13+
14+ @ Override
15+ public void act (PartyMember actor , Action action ) {
16+ for (PartyMember member : members ) {
17+ if (member != actor ) {
18+ member .partyAction (action );
19+ }
20+ }
21+ }
22+
23+ @ Override
24+ public void addMember (PartyMember member ) {
25+ members .add (member );
26+ member .joinedParty (this );
27+ }
28+
29+ // somebody hunts for food, call for dinner
30+
31+ // somebody spots enemy, alert everybody
32+
33+ // somebody finds gold, deal the gold with everybody
34+
35+ // somebody tells a tale, call everybody to listen
36+
37+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface PartyMember {
4+
5+ void joinedParty (Party party );
6+
7+ void partyAction (Action action );
8+
9+ void act (Action action );
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public abstract class PartyMemberBase implements PartyMember {
4+
5+ protected Party party ;
6+
7+ @ Override
8+ public void joinedParty (Party party ) {
9+ System .out .println (this + " joins the party" );
10+ this .party = party ;
11+ }
12+
13+ @ Override
14+ public void partyAction (Action action ) {
15+ String s = this + " " ;
16+ switch (action ) {
17+ case ENEMY :
18+ s = s + "runs for cover" ;
19+ break ;
20+ case GOLD :
21+ s = s + "takes his share of the gold" ;
22+ break ;
23+ case HUNT :
24+ s = s + "arrives for dinner" ;
25+ break ;
26+ case TALE :
27+ s = s + "comes to listen" ;
28+ break ;
29+ default :
30+ break ;
31+ }
32+ System .out .println (s );
33+ }
34+
35+ @ Override
36+ public void act (Action action ) {
37+ if (party != null ) {
38+ System .out .println (this + " " + action .toString ());
39+ party .act (this , action );
40+ }
41+ }
42+
43+ @ Override
44+ public abstract String toString ();
45+
46+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class Rogue extends PartyMemberBase {
4+
5+ @ Override
6+ public String toString () {
7+ return "Rogue" ;
8+ }
9+
10+ }
You can’t perform that action at this time.
0 commit comments