Iterator
When we were discussing the Composite design pattern in the previous chapter, we noted that the design pattern felt a bit incomplete. Now is the time to reunite the twins separated at birth. Much like Arnold Schwarzenegger and Danny DeVito, they're very different, but complement each other well.
One, two... many
We're back to our squads and platoons in our CatsCraft 2: Revenge of the Dogs strategy game.
As you may remember from the previous chapter, Squad
consists of InfantryUnits
:
interface InfantryUnit class Squad(val infantryUnits: MutableList<InfantryUnit> = mutableListOf()) { }
Each squad should also get a commander now.
The commander of a squad called Sergeant
is also an InfantryUnit
:
class Squad(...) { val commander = Sergeant() } class Sergeant: InfantryUnit
Please disregard the fact that our sergeant doesn't have a name and gets created on the fly. We're two days short of releasing this game and beating the competition. Names are not important now.
The platoon...