I'm new to Pharo and I'm having trouble grasping some concepts, specifically subclassResponsibilty
.
I have the following objecttree:
AbstractDictionary
--TreeBasedDictionary (not abstract)
--AbstractArrayDictionary
----SimpleDictionary (not abstract)
----FastDictionary (not abstract)
All 3 implement the operations at:put:
and at:
(and more). I understood from class today that I can pull up at:put:
to the AbstractDictionary
with the following implementation:
at: thisKey put: thisValue
"insert new key / value pair"
^self
at: thisKey
put: thisValue
duplicate: [self error:'duplicate key']
Then I implemented a metod at:put:duplicate:
in TreeBasedDictionary and AbstractArrayDictionary
(because the implementation is the same for the latter's subclasses). Then, when I call at:put:
on an object it won't find it in the instance of either TreeBasedDictionary , SimpleDictionary or FastDictionary
but when looking up the tree it finds this method in the superclass (AbstractDictionary). This will then call self with at:put:duplicate:
. This method is implemented by the 3 afore mentioned classes, and then the self which is pointing to the instance of the class that made the initial call is sent the message to execute at:put:duplicate:
.
Now, AbstractArrayDictionary,SimpleDictionary and FastDictionary
all have a method named size
. Both classes at the bottom of the inheritence tree implement this method. AbstractArrayDictionary
implements this method as follows
size
"dictionary size"
self subclassResponsibility
Suppose I implemented this method for my TreeBasedDictionary
, I suppose I should implement the size method in my AbstractDictionary
class as shown above.
My questions: Why do I need to do it like that, and not like this:
size
"dictionary size"
^self size
And if so, do I remove the method from AbstractArrayDictionary
and why?
put:at:
and then useat:put:
in the example. Also you have bothat:put:duplicate:
andput:at:duplicate:
. Are you sure that you haven't done any mistakes? – Northerner