Should I Add an Instance Variable Named 'type'?


When an ivar is named "type" or includes "type" in the name that is often an indicator that we need more object types (e.g., classes).

Let us say we had an object "Shape" with ivar "type".

And let us say we have a couple objects:


The problem is that Circle and Rectangle have different behaviors.

This would be a better design:

Shape			<----------- super-type (e.g., superclass)
	Circle		<---------- sub-type (e.g., subclass)
	Rectangle	<---------- sub-type


How Far Can We Simplify?


In the example, we were able to eliminate CircularTurtle and RectangleTurtle because they added zero sub-components (ivars) and zero behaviors (methods) to the super-class -- i.e. there is no reason for them -- they added nothing.

Our specialized structures have a similar case -- LinkedStack and ArrayedStack add nothing to what a simple "Stack" type would do. And similar "Queue", "Deque", and "Bag".

Thus, after elimination, we end up with one super-type and four sub-types:

SpecializedStructure	<----------- super-type (e.g., superclass)
	Stack				<---------- sub-type (e.g., subclass)
	Queue				<---------- sub-type
	Deque				<---------- sub-type
	Bag					<---------- sub-type


Now we might then ask "Can we get rid of all four sub-types?"

We explore the question, but we see that:


So Stack, Queue, Deque, and Bag do have their own behaviors so we can not eliminate them -- we need their behaviors.