Refactoring Day 4 : Push Down Method

Yesterday we looked at the pull up refactoring to move a method to a base class so mutiple derived classes can use a method. Today we look at the opposite. Here is the code before the refactoring:

   1: public abstract class Animal
   2: {
   3:     public void Bark()
   4:     {
   5:         // code to bark
   6:     }
   7: }
   8:  
   9: public class Dog : Animal
  10: {
  11: }
  12:  
  13: public class Cat : Animal
  14: {
  15: }

So here we have some code with a base class that has a Bark method. Perhaps at one time our cat could bark, but now we no longer need that functionality on the Cat class. So we “Push Down” the Bark method into the Dog class as it is no longer needed on the base class but perhaps it is still needed when dealing explicitly with a Dog. At this time, it’s worthwhile to evaluate if there is any behavior still located on the Animal base class. If not, it is a good opportunity to turn the Animal abstract class into an interface instead as no code is required on the contract and can be treated as a marker interface.

   1: public abstract class Animal
   2: {
   3: }
   4:  
   5: public class Dog : Animal
   6: {
   7:     public void Bark()
   8:     {
   9:         // code to bark
  10:     }
  11: }
  12:  
  13: public class Cat : Animal
  14: {
  15: }

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.
  • remi bourgarel

    thank you for these examples , even if it seems a bit too much simple, we always have to start by the most simple.

  • http://www.lostechies.com/members/schambers/default.aspx schambers

    @remi

    Yeah, I know this is extremely basic, but I was trying to keep all of these posts as short as possible and not too complex. Hopefully everyone can see the aim here and apply it to larger scenarios

  • Mordy

    Aye, I appreciate seeing the basics specified too – especially with ‘conceptual’ examples like this.

  • http://hinternets.blogspot.com/ Quick Joe Smith

    Sometimes there is such a thing as too simple, or too contrived. It can obscure the real value of the technique. I remember feeling the same way about most tutorials concerning C# delegates. The tutorials focused on simplicity to the exclusion of meaningful context. It took me a while before I found an example that didn’t leave me wondering why you wouldn’t just call the encapsulated method directly instead.

    So, yes, I think some more real-world examples would be more educational than using dogs and cats.

  • nfma

    @Quick Joe Smith, well, I don’t know how you’d make this example any better by adding noise to it.

    Pull down method is this simple.

    If you’re not happy with Cat and Dogs, you can always rename the classes and methods to what ever you want… :)

  • Archy_yu

    i like it,and i am translating it into Chinese,to share with my friends. for give me for not ask for your admission.
    i represent all my friends say thanks.