Problem Statement
Work out the logic for the method method "drawAll" that will draw a collection of graphic elements.
Algorithm - High Level
Nice and simple
drawAll(elements) {
	Iterate over collection of graphic elements {
		draw each element
	}
}
The Sticky Problem
We have a bit of a sticky problem:

The parameter "elements" can be so many different types. We have to be able to handle all the different types.
Solution Try #1
😢

Try #1 is scary. Think of all that work. And how do we handle Etc?
drawAll(elements) {
	If the type of "elements" is a SortedCollection
		drawSorted(elements)
	Else if the type of "elements" is an OrderedCollection
		drawOrdered(elements)
	Else if the type of "elements" is an LinkedList
		drawLinkedList(elements)
	Else if the type of "elements" is a SuperFastCollection
		drawSuperFast(elements)
	Else if the type of "elements" is a DynamicArray
		drawDynamicArray(elements)
	Etc...
}
Solution Try #2
First let's take a closer look at our logic.

Note that we send only two messages (highlighted) to the collection "elements".

What if we require that (all) the different collection types must be able to handle the messages "size" and "get".
drawAll(elements) {
	let nextIndex = 0
	while (nextIndex > elements.size()) {
		let nextDrawingElem = elements.get(nextIndex)
		this.draw(nextDrawingElem)
}
Generalization
What we did is generalize the many different collection types into one general type which we call "Collection".

We could call the new general type whatever we like -- Collection, List, etc
Collection
	size()
		//returns size of collection
	get(anIndex)
		//returns element at arg "anIndex"
What to the Specialized Need to Do?
As long as each specialized type supports "size" and "get", then we can say:

  • SortedCollection "is-a" Collection
  • OrderedCollection "is-a" Collection
  • etc.