How to implement the template method design pattern in C#


Design patterns are proven solutions to recurring problems and complexities in software development. Design patterns fall into three categories: creational, structural, and behavioral. Creational patterns are used to create and manage instances of classes. Structural patterns are used to realize the relationships among the entities. Behavioral design patterns deal with object collaboration and delegation of responsibilities.

Note that the template method pattern belongs to the behavioral design pattern category. In the sections that follow, we will examine this design pattern in detail.

Why use the template method pattern?

The template method design pattern is useful when you have a skeleton of an algorithm defined in a base class and a small part of the algorithm may vary and is implemented with variation in a subclass, i.e., the concrete class. In essence, the template of the algorithm is defined in the base class and while certain steps of the algorithm (the part that variesthose that vary from the base class) is are re-defined in a concrete class.

The defines the as follows: