Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active August 21, 2023 17:49
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 mcsee/817257ca2966c8f2381dcf9887dfa1a4 to your computer and use it in GitHub Desktop.
Save mcsee/817257ca2966c8f2381dcf9887dfa1a4 to your computer and use it in GitHub Desktop.
public abstract class OrderState { }
public final class OrderStatePending extends OrderState { }
// This is a polymorphic hierarchy with different behavior
// An enum is not enough to model state
public final class Order {
public Order(LinkedList<int> items) {
LinkedList<int> items = items;
OrderState state = new OrderStatePending();
}
public function changeStatus(OrderState newState) {
OrderState state = newState;
}
public function confirm() {
state.Confirm(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment