1

Over on StackOverflow I found myself recommending the following pattern. I see it used fairly often in production but I don't remember having seen any articles or blog posts discussing its use.

The gist is that you provide an overridable implementation method which does not form part of the public API. The public-facing method can not be overriden and may do some validation, assertions etc. before (or after) calling the actual implementation function. Maybe under certain conditions it will decide not to call it at all.

It's hard to describe in words so hopefully the following code snippet should be understood by those without any Java experience. The concept should be applicable to any polymorphic language.

abstract class Animal        //may be abstract or concrete
{
    final public void eat()
    {
        if (isHungry)
        {
            eatImpl();
        }
    }

    abstract protected void eatImpl();
}

class Dog extends Animal
{
     @Override
     protected void eatImpl()
     {
         // Nom nom nom
     }
}

In this case, we don't want any Animals to eat if they're not hungry - forcing a dog to eat when he wasn't hungry would not be very nice.

Does this pattern have a name? What is it called?

I've always referred to it as 'deferred methods' which I don't think is very descriptive. While I typed this question out I came up with 'AOP for Dummies'.


I dislike the xxxImpl() naming convention but it seems the most commonly used in this kind of situation.

closed as primarily opinion-based by gnat, Thomas Owens Apr 5 '17 at 18:21

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    not sure what this is called but you could replace it with hole-in-the-middle – jk. Apr 4 '17 at 14:59
  • @jk. Interesting. I haven't heard of that. For whatever reason, it seems to be more related to the C# world, though there's no practical reason that should be the case. It also seems to often (though not always) be related to functions which take functions, lambdas etc as arguments. That said, the concept is very much the same. – Michael Apr 4 '17 at 15:07
  • 1
    Re the name, I also see doXxx() a lot. – yshavit Apr 4 '17 at 17:16
5

This is the template method design pattern. From Wikipedia:

In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses.[1] It lets one redefine certain steps of an algorithm without changing the algorithm's structure.[2]

https://en.wikipedia.org/wiki/Template_method_pattern

As for the naming convention xxxImpl(), I agree. Naming any implementation "impl" is a red flag in my mind as it suggests an abstraction that can only have one implementation, which in my mind, is very often overkill.

  • Yep, that's the one! I actually wasn't expecting this to fall into the category of any of the "major" design patterns. – Michael Apr 4 '17 at 15:12
  • I usually name mine DoEat() in C# and onEat() in Java. – Chris Wohlert Apr 5 '17 at 10:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.