Refactoring Day 17 : Extract Superclass
Today’s refactoring is from Martin Fowler’s refactoring catalog. You can find the original description here
This refactoring is used quite often when you have a number of methods that you want to “pull up” into a base class to allow other classes in the same hierarchy to use. Here is a class that uses two methods that we want to extract and make available to other classes.
1: public class Dog
2: {
3: public void EatFood()
4: {
5: // eat some food
6: }
7:
8: public void Groom()
9: {
10: // perform grooming
11: }
12: }
After applying the refactoring we just move the required methods into a new base class. This is very similar to the [pull up refactoring], except that you would apply this refactoring when a base class doesn’t already exist.
1: public class Animal
2: {
3: public void EatFood()
4: {
5: // eat some food
6: }
7:
8: public void Groom()
9: {
10: // perform grooming
11: }
12: }
13:
14: public class Dog : Animal
15: {
16: }
This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.