Introduction
The Template Method pattern is a behavioral design pattern from the classic Gang of Four Design Patterns book. In this article, I'll be offering a way to conceptualize and implement this pattern in C#, citing framework design as a quite familiar example. This pattern is really useful in scenarios where different methods need to be called in a specific sequence in order to work correctly. We don't want to leave that kind of responsibility to the consumer of our code. We want them to be able to pull up their code completion and use what's available to them without fear.
For implementing this pattern, we will be designing a simple Read, Evaluate, Print Loop (REPL) framework for command-line applications. I'm using this example because we actually see this design pattern all the time in our regular day-to-day life. We're just typically using this pattern as consumers of some framework.
The goal of our project is to allow the consumer of our framework, the application developer, to describe commands and actions for the user to perform. The Loop itself will just happen for them automatically. (The application developer should write code to describe what happens in their application, not focus on how it happens.)
The sign that you're using good abstractions, is when your code describes what happens, and not how it happens.
The Template Method design pattern is sometimes referred to as the Hollywood Principle, or "Don't call us, we'll call you!" This is because the consumer of our framework will not be responsible for invoking vital methods to make our application work correctly--but rather overriding methods that will be invoked behind the scenes.
Example Xamarin Forms
[Android sub-project, MainActivity.cs]
protected override void OnCreate(Bundle savedInstanceState)
{
// ...
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
// ...
}
[Xamarin.Forms shared project, App.xaml.cs]
public partial class App : Application
{
public App()
{
// ...
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
You can recognize the template methods by their "On" prefix. This convention signifies that these methods are triggered automatically when the appropriate events occur in the application. This type of code is very familiar because it's used everywhere in framework design. But have you ever wondered how to implement this yourself?
Let's do it.
Implementation
Our framework will consist of a single Application base class that the consumer can inherit from to describe their application.
public abstract class Application
{
We create it as an abstract class because we can't allow this class to be instantiated. It needs to be inherited, for this pattern to work.
We are not using an interface since we will be providing behaviors in this class.
First, we'll define a flag that we'll use later to quit the loop.
public abstract class Application
{
private bool _running = false;
(We'll set this to true later when our Application starts.)
Next, we will declare our template methods.


Mahesh ChandPosted Mar 30, 2020, 6:27 AM
Welcome aboard, Sean. Nice start. Best