File tree Expand file tree Collapse file tree 6 files changed +180
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 6 files changed +180
-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 >servant</artifactId >
12+ <version >1.0-SNAPSHOT</version >
13+ <name >servant</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+ import java .util .ArrayList ;
4+
5+
6+ /**
7+ * Servant offers some functionality to a group of classes without defining that functionality in each of them.
8+ * A Servant is a class whose instance provides methods that take care of a desired service,
9+ * while objects for which the servant does something, are taken as parameters.
10+ *
11+ */
12+ public class App {
13+ static Servant jenkins = new Servant ("Jenkins" );
14+ static Servant travis = new Servant ("Travis" );
15+
16+ public static void main ( String [] args ){
17+ scenario (jenkins , 1 );
18+ scenario (travis , 0 );
19+ }
20+
21+
22+ public static void scenario (Servant servant , int compliment ){
23+ King k = new King ();
24+ Queen q = new Queen ();
25+
26+ ArrayList <Royalty > guests = new ArrayList <>();
27+ guests .add (k );
28+ guests .add (q );
29+
30+ //feed
31+ servant .feed (k );
32+ servant .feed (q );
33+ //serve drinks
34+ servant .giveWine (k );
35+ servant .giveWine (q );
36+ //compliment
37+ servant .GiveCompliments ( guests .get (compliment ) );
38+
39+ //outcome of the night
40+ for (Royalty r : guests )
41+ r .changeMood ();
42+
43+ //check your luck
44+ if ( servant .checkIfYouWillBeHanged (guests ) )
45+ System .out .println (servant .name + " will live another day" );
46+ else
47+ System .out .println ("Poor " + servant .name + ". His days are numbered" );
48+ }
49+
50+
51+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class King implements Royalty {
4+ private boolean isDrunk = false , isHungry = true , isHappy = false ;
5+ private boolean complimentReceived = false ;
6+
7+ @ Override
8+ public void feed () {
9+ isHungry = false ;
10+ }
11+
12+ @ Override
13+ public void giveDrink () {
14+ isDrunk = true ;
15+ }
16+
17+ public void receiveCompliments (){
18+ complimentReceived = true ;
19+ }
20+
21+ @ Override
22+ public void changeMood () {
23+ if (!isHungry && isDrunk ) isHappy = true ;
24+ if ( complimentReceived ) isHappy = false ;
25+ }
26+
27+ @ Override
28+ public boolean getMood () {
29+ return isHappy ;
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class Queen implements Royalty {
4+ private boolean isDrunk = true , isHungry = false , isHappy = false ;
5+ private boolean isFlirty = true , complimentReceived = false ;
6+
7+ @ Override
8+ public void feed () {
9+ isHungry = false ;
10+ }
11+
12+ @ Override
13+ public void giveDrink () {
14+ isDrunk = true ;
15+ }
16+
17+ public void receiveCompliments (){
18+ complimentReceived = true ;
19+ }
20+
21+ @ Override
22+ public void changeMood () {
23+ if ( complimentReceived && isFlirty && isDrunk ) isHappy = true ;
24+ }
25+
26+ @ Override
27+ public boolean getMood () {
28+ return isHappy ;
29+ }
30+
31+ public void setFlirtiness (boolean f ){
32+ this .isFlirty = f ;
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ interface Royalty {
4+ public void feed ();
5+ public void giveDrink ();
6+ public void changeMood ();
7+ public void receiveCompliments ();
8+ public boolean getMood ();
9+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ import java .util .ArrayList ;
4+
5+ public class Servant {
6+ public String name ;
7+
8+ public Servant (String name ){
9+ this .name = name ;
10+ }
11+
12+ public void feed (Royalty r ){
13+ r .feed ();
14+ }
15+
16+ public void giveWine (Royalty r ){
17+ r .giveDrink ();
18+ }
19+
20+ public void GiveCompliments (Royalty r ){
21+ r .receiveCompliments ();
22+ }
23+
24+ public boolean checkIfYouWillBeHanged (ArrayList <Royalty > tableGuests ){
25+ boolean anotherDay = true ;
26+ for ( Royalty r : tableGuests )
27+ if ( !r .getMood () ) anotherDay = false ;
28+
29+ return anotherDay ;
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments