Skip to content

Instantly share code, notes, and snippets.

@nschlimm
Created January 30, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nschlimm/94ce85f9cc553b2ae9546031162504b9 to your computer and use it in GitHub Desktop.
Save nschlimm/94ce85f9cc553b2ae9546031162504b9 to your computer and use it in GitHub Desktop.
// shopping basket with state pattern
public static class ShoppingBasket {
private String orderNo;
private List<String> articleNumbers = new ArrayList<>();
private UpdateState state = UpdateState.UPDATEABLE;
public void add(String articleNumber) {
articleNumbers.add(state.set(articleNumber));
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = state.set(orderNo);
}
public void order() {
// some ordering logic and if succeeded, change state
state = UpdateState.READONLY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment