# Stack and Deck

**URL:** <https://forum.vassalengine.org/t/stack-and-deck/10767>\
**Category:** Development\
**Created:** [June 22, 2020, 11:37pm UTC](https://forum.vassalengine.org/t/stack-and-deck/10767 "2020-06-22T23:37:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flint1b](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/flint1b/32/246_2.png) [@Flint1b](https://forum.vassalengine.org/u/Flint1b)\
**Post date:** [June 22, 2020, 11:37pm UTC](https://forum.vassalengine.org/t/stack-and-deck/10767/1 "2020-06-22T23:37:13Z")

</div>

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:

```auto
/** 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) {
    }
  }
}
```
