1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| public enum StateStatus { NORMAL,ALARM,URGENT,SUFFICIENT }
public class State { private StateStatus stateStatus;
public State(StateStatus stateStatus) { this.stateStatus = stateStatus; }
public void handleUpdate(Publisher publisher) { statusUpdate(); }
public void handleReduce(Publisher publisher) { statusUpdate(); }
private void statusUpdate(){ } }
public abstract class Publisher { private State state;
public Publisher(State state ) { this.state = state; }
}
public class Storage extends Publisher {
public Storage(int limit, HashMap storageMap, String name, Mediator mediator) { super(new State(StateStatus.NORMAL),limit, storageMap, name, mediator); }
@Override public void updateItem(String itemName, int count) { this.getStorageMap().computeIfPresent(itemName, (k, v) -> v += count); System.out.println(this.getStorageMap().toString()); }
@Override public void notify(String itemName, int count, State state) { mediator.notify(this, this.getState()); }
public void add(String itemName, int count) { this.getStorageMap().put(itemName, count); this.getState().handleUpdate(this); }
public void reduce(String itemName) { int currentVal = this.getStorageMap().get(itemName); this.getStorageMap().put(itemName, currentVal - 1); this.notify(itemName, currentVal - 1); this.getState().handleReduce(this); } }
|