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.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Chambers

I am a Senior software developer from Palm Coast, Florida. An advocate of Domain Driven Design, Behavior Driven Development, creator of FluentMigrator and community activist. I am married to my beautiful wife Erin and am the proud father of two wonderful children. I currently reside at ACI, a local insurance industry/mortgage software company that excels in creating solutions using Agile methodologies.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.