Template Method Pattern

The template method pattern is a behavioral pattern that defines a template structure and deferred implementation to subclasses.

Redefine the content of a template in a subclass without changing the template structure.

role

1. Abstract Class

The template method is implemented and the framework of the algorithm is defined.

2. Concrete Class

Implement abstract methods in abstract classes to complete the algorithm.

The sample



The TemplateMethod namespace contains the DataSource DataSource abstraction class, which has instance methods, abstraction methods, and hook methods (IsNotJson). The ShowChart method uses the DataSource to display a chart. This example uses this example to explain the implementation essentials of the template method pattern.

public abstract class DataSource {

    protected abstract void FetchSource();

    protected virtual bool IsNotJson() {
        return true;
    }

    protected abstract void Convert2Json();

    protected abstract void ShowData();

    public void ShowChart() {
        FetchSource();
        if (IsNotJson()) {
            Convert2Json();
        }
        ShowData();
        Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); }}Copy the code

The data source abstraction base class DataSouce contains the FetchSource method, IsNotJson method, Convert2Json method, and ShowChart method.

public class TextData : DataSource {
 
    protected override void FetchSource() {
        Console.WriteLine($"Fetch data from {this.ToString()}!");
    }
 
    protected override void Convert2Json() {
        Console.WriteLine($"Convert {this.ToString()} to Json!");
    }
 
    protected override void ShowData() {
        Console.WriteLine($"Show data in chart control!"); }}Copy the code

TextData class.

public class BinaryData : DataSource {
 
    protected override void FetchSource() {
        Console.WriteLine($"Fetch data from {this.ToString()}!");
    }
 
    protected override void Convert2Json() {
        Console.WriteLine($"Convert {this.ToString()} to Json!");
    }
 
    protected override void ShowData() {
        Console.WriteLine($"Show data in chart control!"); }}Copy the code

BinaryData source BinaryData class.

public class JsonData : DataSource {
 
    protected override void FetchSource() {
        Console.WriteLine($"Fetch data from {this.ToString()}!");
    }
 
    protected override bool IsNotJson() {
        return false;
    }
 
    protected override void Convert2Json() {
        Console.WriteLine("This line can not be reached!");
        Console.WriteLine("There's no need to convert data!");
    }
 
    protected override void ShowData() {
        Console.WriteLine($"Show data in chart control!"); }}Copy the code

Json data source JsonData class.

public class CloudData : DataSource {
 
    protected override void FetchSource() {
        Console.WriteLine($"Fetch data from {this.ToString()}!");
    }
 
    protected override void Convert2Json() {
        Console.WriteLine($"Convert {this.ToString()} to Json!");
    }
 
    protected override void ShowData() {
        Console.WriteLine($"Show data in chart control!"); }}Copy the code

CloudData source CloudData class.

public class Program {

    private static DataSource _dataSource = null;

    public static void Main(string[] args) {
        _dataSource = new TextData();
        _dataSource.ShowChart();

        _dataSource = new BinaryData();
        _dataSource.ShowChart();

        _dataSource = new JsonData();
        _dataSource.ShowChart();

        _dataSource = newCloudData(); _dataSource.ShowChart(); Console.ReadKey(); }}Copy the code

This is the caller’s code, and here is the output of the case:

Fetch data from TemplateMethod.TextData!
Convert TemplateMethod.TextData to Json!
Show data in chart control!
----------------------------------
Fetch data from TemplateMethod.BinaryData!
Convert TemplateMethod.BinaryData to Json!
Show data in chart control!
----------------------------------
Fetch data from TemplateMethod.JsonData!
Show data in chart control!
----------------------------------
Fetch data from TemplateMethod.CloudData!
Convert TemplateMethod.CloudData to Json!
Show data in chart control!
----------------------------------
Copy the code

advantages

1, improve code reuse, can put the same part of the code in the abstract parent class; 2, improve the extensibility, put different code into different subclasses, through the extension of the subclass to add new behavior; 3. Reverse control is implemented by calling the operations of its subclasses through a parent class and adding new behaviors by extending the subclasses.

disadvantages

1, the introduction of abstract classes, each different implementation needs a subclass to achieve, resulting in an increase in the number of classes, thus increasing the complexity of system implementation.

Usage scenarios

1. Implement the invariant part of an algorithm at once, and leave the variable behavior to subclasses to implement; 2. Common behaviors in each subclass should be extracted and grouped into a common parent class to avoid code duplication.