Adding Servant Design pattern classes

This commit is contained in:
Peter G. Sideris 2014-12-01 14:23:08 +01:00
parent 710d31b2a2
commit 187cccca99
6 changed files with 180 additions and 0 deletions

23
servant/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>servant</artifactId>
<version>1.0-SNAPSHOT</version>
<name>servant</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,51 @@
package com.iluwatar;
import java.util.ArrayList;
/**
* Servant offers some functionality to a group of classes without defining that functionality in each of them.
* A Servant is a class whose instance provides methods that take care of a desired service,
* while objects for which the servant does something, are taken as parameters.
*
*/
public class App {
static Servant jenkins = new Servant("Jenkins");
static Servant travis = new Servant("Travis");
public static void main( String[] args ){
scenario(jenkins, 1);
scenario(travis, 0);
}
public static void scenario(Servant servant, int compliment){
King k = new King();
Queen q = new Queen();
ArrayList<Royalty> guests = new ArrayList<>();
guests.add(k);
guests.add(q);
//feed
servant.feed(k);
servant.feed(q);
//serve drinks
servant.giveWine(k);
servant.giveWine(q);
//compliment
servant.GiveCompliments( guests.get(compliment) );
//outcome of the night
for(Royalty r : guests)
r.changeMood();
//check your luck
if( servant.checkIfYouWillBeHanged(guests) )
System.out.println(servant.name + " will live another day");
else
System.out.println("Poor " + servant.name + ". His days are numbered");
}
}

View File

@ -0,0 +1,31 @@
package com.iluwatar;
public class King implements Royalty{
private boolean isDrunk = false, isHungry = true, isHappy = false;
private boolean complimentReceived = false;
@Override
public void feed() {
isHungry = false;
}
@Override
public void giveDrink() {
isDrunk = true;
}
public void receiveCompliments(){
complimentReceived = true;
}
@Override
public void changeMood() {
if(!isHungry && isDrunk) isHappy = true;
if( complimentReceived ) isHappy = false;
}
@Override
public boolean getMood() {
return isHappy;
}
}

View File

@ -0,0 +1,35 @@
package com.iluwatar;
public class Queen implements Royalty{
private boolean isDrunk = true, isHungry = false, isHappy = false;
private boolean isFlirty = true, complimentReceived = false;
@Override
public void feed() {
isHungry = false;
}
@Override
public void giveDrink() {
isDrunk = true;
}
public void receiveCompliments(){
complimentReceived = true;
}
@Override
public void changeMood() {
if( complimentReceived && isFlirty && isDrunk ) isHappy = true;
}
@Override
public boolean getMood() {
return isHappy;
}
public void setFlirtiness(boolean f){
this.isFlirty = f;
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
interface Royalty {
public void feed();
public void giveDrink();
public void changeMood();
public void receiveCompliments();
public boolean getMood();
}

View File

@ -0,0 +1,31 @@
package com.iluwatar;
import java.util.ArrayList;
public class Servant {
public String name;
public Servant(String name){
this.name = name;
}
public void feed(Royalty r){
r.feed();
}
public void giveWine(Royalty r){
r.giveDrink();
}
public void GiveCompliments(Royalty r){
r.receiveCompliments();
}
public boolean checkIfYouWillBeHanged(ArrayList<Royalty> tableGuests){
boolean anotherDay = true;
for( Royalty r : tableGuests )
if( !r.getMood() ) anotherDay = false;
return anotherDay;
}
}