Skip to content

Instantly share code, notes, and snippets.

@jordancodurance
Created January 3, 2023 11:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Special case null stack solution for https://www.codurance.com/katalyst/stack
interface Stack {
static Stack create(int size) {
if (size < 0) throw new IllegalStackSizeException();
if (size == 0) return new NullStack();
return new BoundedStack(size);
}
void push(int element);
boolean isEmpty();
// Other operations...
}
class NullStack implements Stack {
@Override public void push(int element) {
throw new StackOverFlowException(); // 0 stack always overflows
}
@Override public boolean isEmpty() {
return true; // 0 stack is always empty
}
}
class BoundedStack implements Stack {
@Override public void push(int element) {
// push code
}
@Override public boolean isEmpty() {
// empty check code
}
}
@jordancodurance
Copy link
Author

For the special case of a null stack, if you are using a language which supports polymorphism, consider creating a base abstract Stack, a BoundedStack with your current implementation, and a NullStack with only the code required for the 0 case. Then provide a way to construct either derivate of the stack based on the size provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment