![这里写图片描述](https://img-blog.csdn.net/20160824110630898)
public abstract class DwarvenMineWorker {
public DwarvenMineWorker() {
}
public void goToSleep() {
System.out.println(name() + " goes to sleep.");
}
public void wakeUp() {
System.out.println(name() + " wakes up.");
}
public void goHome() {
System.out.println(name() + " goes home.");
}
public void goToMine() {
System.out.println(name() + " goes to the mine.");
}
public abstract void work();
public abstract String name();
private void action(Action action)
{
switch(action)
{
case GO_TO_SLEEP:
goToSleep();
break;
case WAKE_UP:
wakeUp();
break;
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
System.out.println("Undefined action");
break;
}
}
public void action(Action...actions)
{
for(Action action:actions)
{
action(action);
}
}
public static enum Action
{
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
}
}
public class DwarvenCartOperator extends DwarvenMineWorker {
public void work() {
System.out.println(name() + " moves gold chunks out of the mine.");
}
public String name() {
return "Dwarf cart operator";
}
}
public class DwarvenGoldDigger extends DwarvenMineWorker {
public void work() {
System.out.println(name() + " digs for gold.");
}
public String name() {
return "Dwarf gold digger";
}
}
public class DwarvenTunnelDigger extends DwarvenMineWorker {
public void work() {
System.out.println(name() + " creates another promising tunnel.");
}
public String name() {
return "Dwarven tunnel digger";
}
}
public class DwarvenGoldmineFacade {
private final List<DwarvenMineWorker> workers;
public DwarvenGoldmineFacade() {
workers = new ArrayList<>();
workers.add(new DwarvenGoldDigger());
workers.add(new DwarvenCartOperator());
workers.add(new DwarvenTunnelDigger());
}
public void startNewDay() {
makeActions(workers,DwarvenMineWorker.Action.WAKE_UP,DwarvenMineWorker.Action.GO_TO_MINE);
}
public void digOutGold() {
makeActions(workers,DwarvenMineWorker.Action.WORK);
}
public void endDay() {
makeActions(workers,DwarvenMineWorker.Action.GO_HOME,DwarvenMineWorker.Action.GO_TO_SLEEP);
}
private void makeActions(Collection<DwarvenMineWorker> workers, Action... actions) {
for(DwarvenMineWorker worker:workers)
{
worker.action(actions);
}
}
}
public class App {
public static void main(String[] args) {
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
facade.startNewDay();
facade.digOutGold();
facade.endDay();
}
}