java进阶设计模式_java设计模式进阶_servant

//

//

// Generated by StarUML(tm) Java Add-In

//

// @ Project : Untitled

// @ File Name : Royalty.java

// @ Date : 2016/9/5

// @ Author :

//

//

public interface Royalty {

public void getFed();

public void getDrink();

public void changeMood();

public void receiveCompliments();

public boolean getMood();

}

//

//

// Generated by StarUML(tm) Java Add-In

//

// @ Project : Untitled

// @ File Name : King.java

// @ Date : 2016/9/5

// @ Author :

//

//

public class King implements Royalty {

public boolean isDrunk;

public boolean isHungry = true;

public boolean isHappy;

public boolean complimentReceived;

@Override

public void getFed() {

isHungry = false;

}

@Override

public void getDrink() {

isDrunk = true;

}

@Override

public void changeMood() {

if(!isHungry && isDrunk)

isHappy = true;

if(complimentReceived)

isHappy = false;

}

@Override

public void receiveCompliments() {

complimentReceived = true;

}

@Override

public boolean getMood() {

return isHappy;

}

}

//

//

// Generated by StarUML(tm) Java Add-In

//

// @ Project : Untitled

// @ File Name : Queen.java

// @ Date : 2016/9/5

// @ Author :

//

//

public class Queen implements Royalty {

public boolean isDrunk = true;

public boolean isHungry;

public boolean isHappy;

public boolean isFlirty = true;

public boolean complimentReceived;

@Override

public void getFed() {

isHungry = false;

}

@Override

public void getDrink() {

isDrunk = true;

}

@Override

public void changeMood() {

if(complimentReceived && isFlirty && isDrunk)

isHappy = true;

}

@Override

public void receiveCompliments() {

complimentReceived = true;

}

@Override

public boolean getMood() {

return isHappy;

}

}

import java.util.List;

//

//

// Generated by StarUML(tm) Java Add-In

//

// @ Project : Untitled

// @ File Name : Servant.java

// @ Date : 2016/9/5

// @ Author :

//

//

public class Servant {

public String name;

public Servant(String name) {

this.name = name;

}

public void feed(Royalty royalty) {

royalty.getFed();

}

public void giveWine(Royalty royalty) {

royalty.getDrink();

}

public void giveCompliments(Royalty royalty) {

royalty.receiveCompliments();

}

public boolean checkIfYouuWillBeChanged(List tableGuests) {

boolean anotherDay = true;

for(Royalty r : tableGuests)

{

if(!r.getMood())

anotherDay = false;

}

return anotherDay;

}

}

import java.util.ArrayList;

import java.util.List;

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);

}

private static void scenario(Servant servant, int compliment) {

King k = new King();

Queen q = new Queen();

List guests = new ArrayList<>();

guests.add(k);

guests.add(q);

servant.feed(k);

servant.feed(q);

servant.giveWine(k);

servant.giveWine(q);

servant.giveCompliments(guests.get(compliment));

for(Royalty r : guests)

r.changeMood();

if(servant.checkIfYouuWillBeChanged(guests))

System.out.println(servant.name + " will live another day");

else

System.out.println("Poor " + servant.name + ". His days are numbered");

}

}

/* Jenkins will live another day Poor Travis. His days are numbered*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值