Reformat rest of the design patterns - Issue #224
This commit is contained in:
@ -4,55 +4,56 @@ 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.
|
||||
* 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.
|
||||
* <p>
|
||||
* In this example {@link Servant} is serving {@link King} and {@link Queen}.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
static Servant jenkins = new Servant("Jenkins");
|
||||
static Servant travis = new Servant("Travis");
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
scenario(jenkins, 1);
|
||||
scenario(travis, 0);
|
||||
}
|
||||
static Servant jenkins = new Servant("Jenkins");
|
||||
static Servant travis = new Servant("Travis");
|
||||
|
||||
/*
|
||||
* Can add a List with enum Actions for variable scenarios
|
||||
* */
|
||||
public static void scenario(Servant servant, int compliment) {
|
||||
King k = new King();
|
||||
Queen q = new Queen();
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
scenario(jenkins, 1);
|
||||
scenario(travis, 0);
|
||||
}
|
||||
|
||||
ArrayList<Royalty> guests = new ArrayList<>();
|
||||
guests.add(k);
|
||||
guests.add(q);
|
||||
/*
|
||||
* Can add a List with enum Actions for variable scenarios
|
||||
*/
|
||||
public static void scenario(Servant servant, int compliment) {
|
||||
King k = new King();
|
||||
Queen q = new Queen();
|
||||
|
||||
//feed
|
||||
servant.feed(k);
|
||||
servant.feed(q);
|
||||
//serve drinks
|
||||
servant.giveWine(k);
|
||||
servant.giveWine(q);
|
||||
//compliment
|
||||
servant.GiveCompliments(guests.get(compliment));
|
||||
ArrayList<Royalty> guests = new ArrayList<>();
|
||||
guests.add(k);
|
||||
guests.add(q);
|
||||
|
||||
//outcome of the night
|
||||
for (Royalty r : guests)
|
||||
r.changeMood();
|
||||
// feed
|
||||
servant.feed(k);
|
||||
servant.feed(q);
|
||||
// serve drinks
|
||||
servant.giveWine(k);
|
||||
servant.giveWine(q);
|
||||
// compliment
|
||||
servant.GiveCompliments(guests.get(compliment));
|
||||
|
||||
//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");
|
||||
}
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
@ -6,34 +6,36 @@ package com.iluwatar.servant;
|
||||
*
|
||||
*/
|
||||
public class King implements Royalty {
|
||||
|
||||
private boolean isDrunk;
|
||||
private boolean isHungry = true;
|
||||
private boolean isHappy;
|
||||
private boolean complimentReceived;
|
||||
|
||||
@Override
|
||||
public void getFed() {
|
||||
isHungry = false;
|
||||
}
|
||||
private boolean isDrunk;
|
||||
private boolean isHungry = true;
|
||||
private boolean isHappy;
|
||||
private boolean complimentReceived;
|
||||
|
||||
@Override
|
||||
public void getDrink() {
|
||||
isDrunk = true;
|
||||
}
|
||||
@Override
|
||||
public void getFed() {
|
||||
isHungry = false;
|
||||
}
|
||||
|
||||
public void receiveCompliments() {
|
||||
complimentReceived = true;
|
||||
}
|
||||
@Override
|
||||
public void getDrink() {
|
||||
isDrunk = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeMood() {
|
||||
if (!isHungry && isDrunk) isHappy = true;
|
||||
if (complimentReceived) isHappy = false;
|
||||
}
|
||||
public void receiveCompliments() {
|
||||
complimentReceived = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMood() {
|
||||
return isHappy;
|
||||
}
|
||||
@Override
|
||||
public void changeMood() {
|
||||
if (!isHungry && isDrunk)
|
||||
isHappy = true;
|
||||
if (complimentReceived)
|
||||
isHappy = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMood() {
|
||||
return isHappy;
|
||||
}
|
||||
}
|
||||
|
@ -6,39 +6,40 @@ package com.iluwatar.servant;
|
||||
*
|
||||
*/
|
||||
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;
|
||||
}
|
||||
private boolean isDrunk = true;
|
||||
private boolean isHungry;
|
||||
private boolean isHappy;
|
||||
private boolean isFlirty = true;
|
||||
private boolean complimentReceived;
|
||||
|
||||
@Override
|
||||
public void getDrink() {
|
||||
isDrunk = true;
|
||||
}
|
||||
@Override
|
||||
public void getFed() {
|
||||
isHungry = false;
|
||||
}
|
||||
|
||||
public void receiveCompliments() {
|
||||
complimentReceived = true;
|
||||
}
|
||||
@Override
|
||||
public void getDrink() {
|
||||
isDrunk = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeMood() {
|
||||
if (complimentReceived && isFlirty && isDrunk) isHappy = true;
|
||||
}
|
||||
public void receiveCompliments() {
|
||||
complimentReceived = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMood() {
|
||||
return isHappy;
|
||||
}
|
||||
@Override
|
||||
public void changeMood() {
|
||||
if (complimentReceived && isFlirty && isDrunk)
|
||||
isHappy = true;
|
||||
}
|
||||
|
||||
public void setFlirtiness(boolean f) {
|
||||
this.isFlirty = f;
|
||||
}
|
||||
@Override
|
||||
public boolean getMood() {
|
||||
return isHappy;
|
||||
}
|
||||
|
||||
public void setFlirtiness(boolean f) {
|
||||
this.isFlirty = f;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,13 +7,13 @@ package com.iluwatar.servant;
|
||||
*/
|
||||
interface Royalty {
|
||||
|
||||
void getFed();
|
||||
void getFed();
|
||||
|
||||
void getDrink();
|
||||
void getDrink();
|
||||
|
||||
void changeMood();
|
||||
void changeMood();
|
||||
|
||||
void receiveCompliments();
|
||||
void receiveCompliments();
|
||||
|
||||
boolean getMood();
|
||||
boolean getMood();
|
||||
}
|
||||
|
@ -8,30 +8,31 @@ import java.util.ArrayList;
|
||||
*
|
||||
*/
|
||||
public class Servant {
|
||||
|
||||
public String name;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public boolean checkIfYouWillBeHanged(ArrayList<Royalty> tableGuests){
|
||||
boolean anotherDay = true;
|
||||
for( Royalty r : tableGuests )
|
||||
if( !r.getMood() ) anotherDay = false;
|
||||
|
||||
return anotherDay;
|
||||
}
|
||||
public String name;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public boolean checkIfYouWillBeHanged(ArrayList<Royalty> tableGuests) {
|
||||
boolean anotherDay = true;
|
||||
for (Royalty r : tableGuests)
|
||||
if (!r.getMood())
|
||||
anotherDay = false;
|
||||
|
||||
return anotherDay;
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.servant.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user