In the code, Deck is a subclass of Stack. A Deck is a Stack.
After getting to know these two better, I don’t agree with this. A Deck is not a Stack.
A Stack can be created with a GamePiece, and sets that GamePiece’s map and position as it’s own map and position. The Deck does no such thing.
A Stack can be expanded. A Deck is never expanded and always returns false from public boolean isExpanded().
A Stack always has type “stack”. A Deck has a variable type which can be passed into its constructor, and apparently uses public String getType() in a completely different manner.
These two do have many similarities, but not enough to warrant an is a relationship.
My suggestion, this is a rough outline:
/** container class for GamePieces, all accessor methods for the stacks/decks contents move here */
class GamePieceContainer {
private List<GamePiece> contents;
public List<GamePiece> getContents()
public Iterator<GamePiece> getIterator()
public Iterator<GamePiece> getReverseIterator()
public Iterator<GamePiece> getVisibleOrderIterator()
public void add(GamePiece p)
public void insertAt(GamePiece p, int pos)
public void remove(GamePiece p)
public void removeAt(GamePiece p)
public int indexOf(GamePiece p)
// more accessor methods
}
interface HasGamePieceContainer {
GamePieceContainer getGamePieceContainer()
}
class Stack implements HasGamePieceContainer {
GamePieceContainer container;
GamePieceContainer getGamePieceContainer()
}
// Deck does NOT extend Stack anymore
class Deck implements HasGamePieceContainer {
GamePieceContainer container;
GamePieceContainer getGamePieceContainer()
}
class StackDeckClient {
void doSomethingWithStackContents(Stack s) {
s.getGamePieceContainer().getContents().forEach(gp -> ...)
}
void doSomethingWithDeckContents(Deck d) {
d.getGamePieceContainer().getContents().forEach(gp -> ...)
}
void doSomethingWithHasGamePieceContainer(HasGamePieceContainer gpc) {
gpc.getGamePieceContainer().getContents().forEach(gp -> ...)
}
void doSomethingWithGamePiece(GamePiece p) {
if (p instanceof HasGamePieceContainer) {
}
// or
if (p instanceof Stack) {
}
else if (p instanceof Deck) {
}
}
}