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
Applicability
Pros and Cons
PROS
CONS
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.”
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.”
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.”
Step 4 Create the static function in the client class file that means creating the template method client class file, which name sets “TemplateClient.”
Step 5 Create the service class file, then inherit the “ITemplateMethodServices” interface class file.
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<>)); Reference
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…