Los Techies : Blogs about software and anything tech!

Refactoring Day 3 : Pull Up Method


The Pull Up Method refactoring is the process of taking a method and “Pulling” it up in the inheritance chain. This is used when a method needs to be used by multiple implementers.

   1: public abstract class Vehicle
   2: {
   3:     // other methods
   4: }
   5:  
   6: public class Car : Vehicle
   7: {
   8:     public void Turn(Direction direction)
   9:     {
  10:         // code here
  11:     }
  12: }
  13:  
  14: public class Motorcycle : Vehicle
  15: {
  16: }
  17:  
  18: public enum Direction
  19: {
  20:     Left,
  21:     Right
  22: }

As you can see, our Turn method is currently only available to the car class, we also want to use it in the motorcycle class so we create a base class if one doesn’t already exist and “pull up” the method into the base class making it available to both. The only drawback is we have increased surface area of the base class adding to it’s complexity so use wisely. Only place methods that need to be used by more that one derived class. Once you start overusing inheritance it breaks down pretty quickly and you should start to lean towards composition over inheritance. Here is the code after the refactoring:

   1: public abstract class Vehicle
   2: {
   3:     public void Turn(Direction direction)
   4:     {
   5:         // code here
   6:     }
   7: }
   8:  
   9: public class Car : Vehicle
  10: {
  11: }
  12:  
  13: public class Motorcycle : Vehicle
  14: {
  15: }
  16:  
  17: public enum Direction
  18: {
  19:     Left,
  20:     Right
  21: }

 

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 03 2009, 07:35 AM by schambers

Comments

31 Days of Refactoring - Sean Chambers - Los Techies : Blogs about software, programming and anything tech! wrote 31 Days of Refactoring - Sean Chambers - Los Techies : Blogs about software, programming and anything tech!
on 08-04-2009 8:03 AM

Pingback from  31 Days of Refactoring - Sean Chambers - Los Techies : Blogs about software, programming and anything tech!

on 08-04-2009 12:48 PM

WCF/WF/Azure Developer Introductions to WCF and WF in .NET 4 Beta 1 Windows Azure and Cloud Computing Posts for 8/3/09+ Agile Refactoring Day 3: Pull Up Method Silverlight Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update Summary

DotNetKicks.com wrote Refactoring Day 3 : Pull Up Method
on 08-04-2009 2:05 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

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:00 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:02 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