Los Techies : Blogs about software and anything tech!

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.

Kick It on DotNetKicks.com
Posted Aug 17 2009, 11:00 AM by schambers

Comments

on 08-17-2009 6:40 PM

Agile/ALT.NET Zen and the Art of Software Development Acceptance Test "Lifecycle" DevLink 2009 - Good News Everyone! Some nice observations on conference organization in general Refactoring Day 17: Extract Superclass Fluent NHibernate 1.0RC

张荣华 wrote 31天重构指南之十七:提取父类
on 10-11-2009 10:14 PM

今天要说的重构来自于Martin Fowler’s 的重构目录,你可以在这里找到原始描述。 这个经常使用的重构的使用场景是在类中有一些方法你想把它们提取到父类中以便同一继承层次的其它类也可以访问这些方...

31 Days of Refactoring « Vincent Leung's .NET Tech Clips wrote 31 Days of Refactoring « Vincent Leung's .NET Tech Clips
on 10-28-2009 9:28 AM

Pingback from  31 Days of Refactoring « Vincent Leung's .NET Tech Clips

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:01 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:03 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems