Template method – It means the program defines an algorithm skeleton in the superclass, which lets subclasses override algorithm-specific steps that are without changing their structure.

From Design Pattern – Behavioral Patterns

Structure

Template method pattern – Class diagram UML

Applicability

  • Using this pattern when the program wants to let the client-sides extend only particular step of an algorithm, but not the whole algorithm or its structure.
  • Using this pattern when the program has several class, which contain almost identical algorithms with some minor differences. As a result, the program might need to modify all classes when the algorithms change.

Pros and Cons

PROS

  • It can let client-sides override only certain parts of a large algorithm, making them less affected by changes in other parts of the algorithms.
  • It can pull the duplicate code into superclass objects.

CONS

  • The provided skeleton of an algorithm may limit some clients.
  • The developers might violate the Liskov Substitution Principle by suppressing a default step implementation via a subclass object.
  • Template methods tend to be harder to maintain the more steps they have.

How to Implement

#1 Basic Sample

static void Main(string[] args)
{
	Console.WriteLine("Same client code can work with different subclasses:");
	Client.ClientCode(new ConcreteClass1());

	Console.Write("\n");

	Console.WriteLine("Same client code can work with different subclasses:");
	Client.ClientCode(new ConcreteClass2());

	Console.ReadKey();
}

class Client
{
	public static void ClientCode(AbstractClass abstractCkass)
	{
		abstractCkass.TemplateMethod();
	}
}

abstract class AbstractClass
{
	public void TemplateMethod()
	{
		BaseOperation1();
		RequestOperation1();
		BaseOperation2();
		Hook1();
		RequestOperation2();
		BaseOperation3();
		Hook2();
	}

	protected void BaseOperation1() => Console.WriteLine("Base operation 1 process");

	protected void BaseOperation2() => Console.WriteLine("Base operation 2 process");

	protected void BaseOperation3() => Console.WriteLine("Base operation 3 process");

	protected abstract void RequestOperation1();
	protected abstract void RequestOperation2();

	protected virtual void Hook1() { }
	protected virtual void Hook2() { }
}

class ConcreteClass1 : AbstractClass
{
	protected override void RequestOperation1()
	{
		Console.WriteLine("ConcreteClass 1 says: Implmeneted Operation1.");
	}

	protected override void RequestOperation2()
	{
		Console.WriteLine("ConcreteClass 2 says: Implmented Operation2. ");
	}
}

class ConcreteClass2 : AbstractClass
{
	protected override void RequestOperation1()
	{
		Console.WriteLine("ConcreteClass 2 says: Implmeneted Operation1.");
	}

	protected override void RequestOperation2()
	{
		Console.WriteLine("ConcreteClass 2 says: Implmented Operation2. ");
	}

	protected override void Hook1()
	{
		Console.WriteLine("ConcreteClass 2 says: Overridden Hook1");
	}
}

#2 Web Application Project

Step 1 Create two interface class object files. One file name sets “ITemplateMethod.” Another file name sets “ITemplateMethodServices.”

Fig 1 ITemplateMethod interface class source code
Fig 2 ITemplateMethodServices interface class source code

Step 2 Create the abstract class file of the template method pattern, which inherits the “ITemplateMethod” file of the interface class. This name sets “TemplateAbstract.”

Fig 3 TemplateAbstract class file source code

Step 3 Create two concrete class files in the Core projects. One concrete class object file name sets “ConcreteSprtTemplateMethod.” Another concrete class object file name sets “ConcreteReverseTemplateMethod.” These class files inherit the abstract class file, “TemplateAbstract.”

Fig 4 ConcreteSortTemplateMethod class file source code
Fig 5 ConcreteReverseTemplateMethod class file source code

Step 4 Create the static function in the client class file that means creating the template method client class file, which name sets “TemplateClient.”

Fig 6 TemplateClient class file source code

Step 5 Create the service class file, then inherit the “ITemplateMethodServices” interface class file.

Fig 7 TemplateMethodServices class file source code

Step 6 RStep 6 Register the template method pattern-related class file and create the template method pattern-related class file in the web project.

// Template Method
services.AddScoped(typeof(ITemplateMethodServices<>), typeof(TemplateMethodServices<>));
Fig 8 Template method pattern result

Reference

Leave a Reply