Skip to content

Commit 1ea04fe

Browse files
committed
Merge pull request iluwatar#20 from sideris/master
Servant pattern
2 parents 710d31b + 7908baa commit 1ea04fe

File tree

14 files changed

+1985
-1
lines changed

14 files changed

+1985
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@
285285
* there is a concurrent access in object creation, e.g. singleton, where you want to create single instance of the same class and checking if it's null or not maybe not be enough when there are two or more threads that checks if instance is null or not.
286286
* there is a concurrent access on a method where method's behaviour changes according to the some constraints and these constraint change within this method.
287287

288+
##Servant
289+
**Intent:** Servant is used for providing some behavior to a group of classes. Instead of defining that behavior in each class - or when we cannot factor out this behavior in the common parent class - it is defined once in the Servant.
290+
291+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/servant/etc/servant.jpg "Servant")
292+
293+
**Applicability:** Use the Servant pattern when
294+
* When we want some objects to perform a common action and don't want to define this action as a method in every class.
295+
296+
288297
# Frequently asked questions
289298

290299
**Q: What is the difference between State and Strategy patterns?**

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>template-method</module>
3737
<module>visitor</module>
3838
<module>double-checked-locking</module>
39+
<module>servant</module>
3940
</modules>
4041

4142
<build>
@@ -53,4 +54,4 @@
5354
</plugins>
5455
</build>
5556

56-
</project>
57+
</project>

servant/etc/servant.jpg

132 KB
Loading

servant/etc/servant.svg

Lines changed: 259 additions & 0 deletions
Loading

servant/etc/servant.xml

Lines changed: 638 additions & 0 deletions
Large diffs are not rendered by default.

servant/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
<artifactId>servant</artifactId>
11+
<name>servant</name>
12+
<url>http://maven.apache.org</url>
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<version>3.8.1</version>
18+
<scope>test</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>

servant/src/etc/servant.jpg

132 KB
Loading

servant/src/etc/servant.svg

Lines changed: 259 additions & 0 deletions
Loading

servant/src/etc/servant.xml

Lines changed: 638 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
* Can add a List with enum Actions for variable scenarios
23+
* */
24+
public static void scenario(Servant servant, int compliment){
25+
King k = new King();
26+
Queen q = new Queen();
27+
28+
ArrayList<Royalty> guests = new ArrayList<>();
29+
guests.add(k);
30+
guests.add(q);
31+
32+
//feed
33+
servant.feed(k);
34+
servant.feed(q);
35+
//serve drinks
36+
servant.giveWine(k);
37+
servant.giveWine(q);
38+
//compliment
39+
servant.GiveCompliments( guests.get(compliment) );
40+
41+
//outcome of the night
42+
for(Royalty r : guests)
43+
r.changeMood();
44+
45+
//check your luck
46+
if( servant.checkIfYouWillBeHanged(guests) )
47+
System.out.println(servant.name + " will live another day");
48+
else
49+
System.out.println("Poor " + servant.name + ". His days are numbered");
50+
}
51+
52+
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 getFed() {
9+
isHungry = false;
10+
}
11+
12+
@Override
13+
public void getDrink() {
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 getFed() {
9+
isHungry = false;
10+
}
11+
12+
@Override
13+
public void getDrink() {
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
interface Royalty {
4+
public void getFed();
5+
public void getDrink();
6+
public void changeMood();
7+
public void receiveCompliments();
8+
public boolean getMood();
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.getFed();
14+
}
15+
16+
public void giveWine(Royalty r){
17+
r.getDrink();
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+
}

0 commit comments

Comments
 (0)