October 13, 2017
Manual Refactoring : Extract Superclass
Consider a situation where we have two or more classes that share a good deal of common features and implementation.
One way of removing the duplication is to extract a superclass - a common base class that puts all the commonality in one place. (A discussion about the merits or otherwise if inheritance vs. composition we'll save for another day. Just go with me for today.)
First of all, let's declare this base class.
NOW RUN THE TESTS!
Now, before we paste in the implementation from one of the other classes, we need to think about what would be pulled up into this base class and what would remain in the subclasses. i.e., What's the same and what's different in each class?
They're almost identical, except that a Dvd has a director, a Book has an author, and an Album has an artist.
We could leave the implementation of summarise() in each class. But that would mean that the bulk of those implementations would be duplicate code. Really, we just want to leave the specialised parts. So let's generalise this method and extract methods for the specialisms.
RUN THE TESTS AFTER EXTRACTING EACH METHOD!
Next, let's copy and paste the implementation of Dvd into Content.
AND RUN THE TESTS!
The plan is to specialise creator() in the subclasses, so let's make that abstract (how you do this will be language-specific.)
RUN THE TESTS!
A class that has one or more abstract methods should not be directly instantiate-able, so we need to make the class abstract, too, using the ABCMeta Python library.
RUN THE TESTS!
Next, we need to remove the specialised code about director in the constructor, leaving subclasses to add those.
So now we have a complete abstract base class that contains all of the duplicated code. Next, we want our 3 specialised classes to extend this base class, which will require us to remove the duplicated code from each one, and invoke the base class's constructor.
Do one subclass at a time, and RUN THE TESTS after each one.
Posted 8 months, 3 days ago on October 13, 2017