Template Method Pattern Concrete example

The Template Method pattern is a cornerstone of Object-Oriented Programming (OOP), embodying the essence of inheritance and polymorphism. It encapsulates a general algorithm within a base class, providing a blueprint for subclasses to follow. This pattern promotes code reusability and consistency, ensuring that all implementations of the algorithm adhere to a common structure.

By offering a standardized approach to performing operations, the Template Method pattern enforces a contract among subclasses, which is crucial for preserving the coherence of the software’s architecture. It also enables the introduction of new behaviors or alterations to existing ones without disrupting other components of the system.

In summary, the Template Method pattern is a pillar of OOP, playing a significant role in the design of adaptable and robust software systems. Its strategic use allows developers to build upon existing structures, fostering a culture of modularity and scalability within the codebase.

Understanding the Template Method Pattern

The Template Method pattern consists of two parts:

  1. An abstract base class (the template) that defines the algorithm’s structure.
  2. Subclasses that implement the missing steps of the algorithm.

The base class declares a method that contains the algorithm’s steps. Some of these steps are implemented directly in the base class, while others are declared as abstract methods, which must be implemented by any concrete subclass.

Implementing the Template Method Pattern

Let’s illustrate the Template Method pattern with a simple example. Suppose we have a document processing system that supports different file formats. We want to define a common process for all documents, but the actual implementation details may vary depending on the format.

First, we define an abstract base class DocumentProcessor:

public abstract class DocumentProcessor
{
    // Template method
    public void ProcessDocument()
    {
        LoadDocument();
        ParseDocument();
        SaveDocument();
    }

    protected abstract void LoadDocument();
    protected abstract void ParseDocument();
    protected abstract void SaveDocument();
}

Next, we create concrete classes for each document format that extend DocumentProcessor and provide their own implementations for the abstract methods:

public class WordProcessor : DocumentProcessor
{
    protected override void LoadDocument()
    {
        Console.WriteLine("Loading Word document...");
    }

    protected override void ParseDocument()
    {
        Console.WriteLine("Parsing Word document...");
    }

    protected override void SaveDocument()
    {
        Console.WriteLine("Saving Word document...");
    }
}

public class PdfProcessor : DocumentProcessor
{
    protected override void LoadDocument()
    {
        Console.WriteLine("Loading PDF document...");
    }

    protected override void ParseDocument()
    {
        Console.WriteLine("Parsing PDF document...");
    }

    protected override void SaveDocument()
    {
        Console.WriteLine("Saving PDF document...");
    }
}

With this setup, we can now process documents of different formats using the same ProcessDocument method defined in the base class:

var wordProcessor = new WordProcessor();
wordProcessor.ProcessDocument();

var pdfProcessor = new PdfProcessor();
pdfProcessor.ProcessDocument();

Conclusion

The Template Method pattern is a valuable design pattern for situations where you need to provide a standardized approach to executing an algorithm, while still allowing for customization in specific steps. By defining the overall structure of an algorithm in a base class, you can ensure consistency across different implementations, making your code more maintainable and easier to understand.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *