Fog Creek Software
Discussion Board




Template Method

I'm just starting to use .NET for my programming chores.  I am wondering what's the best way to implement a template method.  To refresh the memory, a template method is a method that is defined in a superclass that delegates the implementation of certain key steps to derived classes.

Initially, I expected to be able to declare the delegated methods as protected abstract so that subclasses implement these to make the template concrete.  The problem is that the protected access level seems to behave differently than I'd expected.  Subclasses can see protected members of their superclasses, but superclasses cannot see protected methods in their subclasses.

Is there any access level that does what is needed?  Am I missing something?  What's the best way to implement a template method using this technology?

beginner.net
Friday, March 21, 2003

What kind of compiler errors are you seeing? We use this pattern constantly with great success (protected abstract).

Brad (dotnetguy.techieswithcats.com)
Friday, March 21, 2003

Probably I am totally misreading your post, but doesn't a template method call other methods on its container class? Then these methods are usually implemented as protected abstract (in C#), so subclasses must provide an implementation. The template method itself is usually public sealed/final (what's the modifier in C# again?).

The template method is inherited, so the call is always made to the implementing subclass. The subclass provides its own implementation, so there's no need for superclasses to see the sub's protecteds.

This seems to come out wrong, but hope you understand what I mean.

Thomas Eyde
Saturday, March 22, 2003

Thank goodness for GoF! You see now we're all speaking the same language ;-)

Duncan Smart
Thursday, March 27, 2003

*  Recent Topics

*  Fog Creek Home