From 95a259b66f2bef7997988e490de4dcd5ff28b7ec Mon Sep 17 00:00:00 2001 From: Md Saiful Islam Date: Fri, 15 Sep 2023 18:06:47 +0600 Subject: [PATCH 1/2] =?UTF-8?q?issue=20id:=20#2260=20explanation:=20modify?= =?UTF-8?q?=20README.md=20file.=20-=20add=20Explanation=20selection=20-=20?= =?UTF-8?q?add=20Real-world=20example,=20under=20the=20Explanation=20selec?= =?UTF-8?q?tion.=20Example=20express=20the=C2=A0=20the=20king,=20queen=20a?= =?UTF-8?q?nd=20servant=20relationship.=20-=20add=20plain-word,=20wiki=20s?= =?UTF-8?q?ay=20-=20add=20programmatic=20example=20with=20code=20of=C2=A0?= =?UTF-8?q?=20king,=20queen=20and=20servant=20classes.=20-=20add=20console?= =?UTF-8?q?=20output=20by=20running=20code=20at=20local.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- servant/README.md | 215 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 214 insertions(+), 1 deletion(-) diff --git a/servant/README.md b/servant/README.md index bfd06e2bc37a..77743dc4785c 100644 --- a/servant/README.md +++ b/servant/README.md @@ -3,7 +3,7 @@ title: Servant category: Behavioral language: en tag: - - Decoupling + - Decoupling --- ## Intent @@ -11,6 +11,219 @@ 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. +## Explanation + +Real-world example + +> King, Queen, and other royal member of palace need servant to service them for feeding, +> organizing drinks, and so on. + +In plain words + +> Ensures one servant object to give some specific services for a group of serviced classes. + +Wikipedia says + +> In software engineering, the servant pattern defines an object used to offer some functionality +> to a group of classes without defining that functionality in each of them. A Servant is a class +> whose instance (or even just class) provides methods that take care of a desired service, while +> objects for which (or with whom) the servant does something, are taken as parameters. + +**Programmatic Example** + +Md Saiful Islam, Effective Java 2nd Edition p.18 + +Servant class which can give services to other royal members of palace. + +```java +/** + * Servant. + */ +public class Servant { + + public String name; + + /** + * Constructor. + */ + public Servant(String name) { + this.name = name; + } + + public void feed(Royalty r) { + r.getFed(); + } + + public void giveWine(Royalty r) { + r.getDrink(); + } + + public void giveCompliments(Royalty r) { + r.receiveCompliments(); + } + + /** + * Check if we will be hanged. + */ + public boolean checkIfYouWillBeHanged(List tableGuests) { + return tableGuests.stream().allMatch(Royalty::getMood); + } +} +``` + +Royalty is an interface. It is implemented by King, and Queen classes to get services from servant. + +```java +interface Royalty { + + void getFed(); + + void getDrink(); + + void changeMood(); + + void receiveCompliments(); + + boolean getMood(); +} +``` +King, class is implementing Royalty interface. +```java +public class King implements Royalty { + + private boolean isDrunk; + private boolean isHungry = true; + private boolean isHappy; + private boolean complimentReceived; + + @Override + public void getFed() { + isHungry = false; + } + + @Override + public void getDrink() { + 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; + } +} +``` +Queen, class is implementing Royalty interface. +```java +public class Queen implements Royalty { + + private boolean isDrunk = true; + private boolean isHungry; + private boolean isHappy; + private boolean isFlirty = true; + private boolean complimentReceived; + + @Override + public void getFed() { + isHungry = false; + } + + @Override + public void getDrink() { + isDrunk = true; + } + + public void receiveCompliments() { + complimentReceived = true; + } + + @Override + public void changeMood() { + if (complimentReceived && isFlirty && isDrunk && !isHungry) { + isHappy = true; + } + } + + @Override + public boolean getMood() { + return isHappy; + } + + public void setFlirtiness(boolean f) { + this.isFlirty = f; + } + +} +``` + +Then in order to use: + +```java +public class App { + + private static final Servant jenkins = new Servant("Jenkins"); + private static final Servant travis = new Servant("Travis"); + + /** + * Program entry point. + */ + public static void main(String[] args) { + scenario(jenkins, 1); + scenario(travis, 0); + } + + /** + * Can add a List with enum Actions for variable scenarios. + */ + public static void scenario(Servant servant, int compliment) { + var k = new King(); + var q = new Queen(); + + var guests = List.of(k, 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 + guests.forEach(Royalty::changeMood); + + // check your luck + if (servant.checkIfYouWillBeHanged(guests)) { + LOGGER.info("{} will live another day", servant.name); + } else { + LOGGER.info("Poor {}. His days are numbered", servant.name); + } + } +} +``` + +The console output + +``` +Jenkins will live another day +Poor Travis. His days are numbered +``` + + ## Class diagram ![alt text](./etc/servant-pattern.png "Servant") From d46001d2955508468f8140f983efb19a64cd059b Mon Sep 17 00:00:00 2001 From: Md Saiful Islam Date: Fri, 15 Sep 2023 18:27:08 +0600 Subject: [PATCH 2/2] fix: #2260 - remove a line from readme.md --- servant/README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/servant/README.md b/servant/README.md index 77743dc4785c..c6aaf414873d 100644 --- a/servant/README.md +++ b/servant/README.md @@ -3,7 +3,7 @@ title: Servant category: Behavioral language: en tag: - - Decoupling +- Decoupling --- ## Intent @@ -31,8 +31,6 @@ Wikipedia says **Programmatic Example** -Md Saiful Islam, Effective Java 2nd Edition p.18 - Servant class which can give services to other royal members of palace. ```java