Adding Servant Design pattern classes
This commit is contained in:
parent
710d31b2a2
commit
187cccca99
23
servant/pom.xml
Normal file
23
servant/pom.xml
Normal 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>
|
51
servant/src/main/java/com/iluwatar/App.java
Normal file
51
servant/src/main/java/com/iluwatar/App.java
Normal 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
31
servant/src/main/java/com/iluwatar/King.java
Normal file
31
servant/src/main/java/com/iluwatar/King.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
35
servant/src/main/java/com/iluwatar/Queen.java
Normal file
35
servant/src/main/java/com/iluwatar/Queen.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
servant/src/main/java/com/iluwatar/Royalty.java
Normal file
9
servant/src/main/java/com/iluwatar/Royalty.java
Normal 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();
|
||||||
|
}
|
31
servant/src/main/java/com/iluwatar/Servant.java
Normal file
31
servant/src/main/java/com/iluwatar/Servant.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user