title: What is the Best Way to Extend Functionality?
body: <p>I've run into a situation in which I was to extend the functionality of a given class, but I'm not sure of the best way to go about this. I started by invoking functionality "upwards" and have now switched to "downwards", but I see issues with both. Let me explain what I mean. First, the "upwards" approach:</p>
  
  <pre><code>public class ParentValidator
  {
      public void validate() {
          // Some code
      }
  }
  
  public class ChildValidator extends ParentValidator
  {
      @Override
      public void validate() {
          super.validate();
          // Some code
      }
  }
  
  public class GrandchildValidator extends ChildValidator
  {
      @Override
      public void validate() {
          super.validate();
          // Some code
      }
  }
  </code></pre>
  
  <p>This functions perfectly well, but it requires that I always remember to place super.validate() in my method body or the logic in the parent class(es) won't be executed. In addition, extension in this manner can be considered "unsafe" due to the fact that a child class could actually replace/modify the code defined in the parent class. This is what I call invoking methods "upwards" because I'm invoking methods from higher level classes as I go.</p>
  
  <p>To counter these shortfalls, I decided to make ParentValidator.validate() final and have it invoke a different method. Here's what my code was modified to:</p>
  
  <pre><code>public class ParentValidator
  {
      public final void validate() {
          // Some code
  
          subValidate();
      }
  
      protected void subValidate() {}
  }
  
  public class ChildValidator extends ParentValidator
  {
      @Override
      public final void subValidate() {
          // Some code
  
          subSubValidate();
      }
  
      protected void subSubValidate() {}
  }
  
  public class GrandchildValidator extends ChildValidator
  {
      @Override
      public void subSubBalidate() {
          // Some code
  
          subSubSubValidate();
      }
  
      protected void subSubSubValidate();
  }
  </code></pre>
  
  <p>This is what I was referring to when I say that I'm calling downwards as each class invokes methods on classes "down" the inheritance chain.</p>
  
  <p>Using this approach, I can be guaranteed that the logic in the parent class(es) will be executed, which I like. However, it doesn't scale well. The more layers of inheritance I have, the uglier it gets. At one level, I think this is very elegant. At two levels, it starts to look shoddy. At three or more, it's hideous.</p>
  
  <p>In addition, just as I had to remember to invoke super.validate() as the first line of any of my children's validate methods, I now have to remember to invoke some "subValidate" method at the end of any of my parent's validate methods, so that didn't seem to get any better.</p>
  
  <p>Is there a better way to do this type of extension that I haven't even touched on. Either of these approaches have some serious flaws and I'm wondering if there's a better design pattern I could be using.</p>