Introduction
Behold! Part two of the Open/Closed principle blog and it's going to be a cracker!Here, I will be covering the template method and how it aids our code with conforming to the Open/Closed principle. These methods and principles are based on Robert C. Martins book - Agile principles, Patterns and Practices in C#.
Part one covered the strategy pattern, I recommend reading it before proceeding.
"It's brilliant!" - me.
Recap
Let's recap slightly on the Open/Closed principle. Remember Robert's all important motto?Open to extension but closed for modification
We have no interest in modifying code in previously created modules. Instead, we would like to create a new module to contain our new logic.
Template Method
Ok, so how can the template method help us?The template method essentially uses inheritance to separate code workflow from implementation.
But as always, there is no better way to explain this but with an example.
class Program { static void Main(string[] args) { Tool screwDriver = new Screwdriver(); Tool spanner = new Spanner(); var bob = new Electrician(screwDriver); var billy = new Electrician(spanner); bob.OperateTool(); billy.OperateTool(); Console.ReadKey(); } }
Similar to what we saw in part one - we have two Tool objects being created and an Operate method being invoked on each of them. This will simply print out text stating that the tool has been used. Do you suspect that Screwdriver and Spanner may possibly be subclasses of Tool? hmm...
Below shows our abstract class Tool:
abstract class Tool { public abstract bool IsToolReady(); public abstract void OperateTool(); public void Operate() { if (IsToolReady()) { OperateTool(); } } }
The Tool class is indeed a parent class which defines an Operate method and two abstract methods. The Operate method is our Template method! This means that the code within defines a logical flow or a code template. It is for the subclasses to provide the code for these methods.
The logical flow in this case is that we want to ensure the tool is ready to be used before using it because the last thing we would want to happen is our power drill exploding.. right?
The Operate method calls our abstract methods which (as you may have noticed) doesn't have any implementation yet. So what do we do?!
Well luckily, these methods are abstract and will need be implemented by a subclass which inherits from our Tool class. For example:
class Spanner : Tool { public override bool IsToolReady() { Console.WriteLine("Spanner has been adjusted and is now ready to be used."); return true; } public override void OperateTool() { Console.WriteLine("Spanner has been used."); } }
Sorry, I know it's not doing anything impressive. But we do however have our implementation for the two abstract methods IsToolReady and OperateTool!
This is essentially the magic to the Template method. The parent defines a predetermined logical flow that calls abstract methods which are implemented in the subclasses. Not much to it!
The logical flow should be true to all subclasses of the parent, otherwise it is not a true child. This logical flow should never have to be altered.
Advantages
The advantages this brings are as follows:
- The Electrician only needs to be aware of the Operate method. This is good abstraction and keeps the Electrician naïve to inner workings of the tool.
- We can now create any tool that inherits from Tool and this can be used in our Electrician without changing any of its code.
- We have a logical workflow which will be enforced on all subclasses. This means that we only need to be concerned with writing code for the abstract methods.
Conclusion
Our code now conforms to the Open/Closed principle. If we needed a new tool such as a PowerDrill then we wouldn't have to be worried about changing the Electrician logic to support it.
The Electrician class is able to support any class that inherits from Tool. This is most ideal if we suspect that it is likely that the tool logic is likely to change. For example, more types of tools are expected to be used by the electrician.
However, subclasses being only concerned with writing implementation for abstract methods means that the programmer may not be aware of how these methods are used. Bare this in mind when using this principle.
So, we have finally created a successful a design that means that any new tools will be an extension to the code and will cause minimum modification to any existing code. The ultimate advantage being that we are less likely to break code when adding new functionality. Well done.