How to implement the model method design pattern in C #

0


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

Note that the model method model belongs to the behavioral design model category. In the following sections, we’ll look at this design pattern in detail.

Why use the model pattern method?

The model method design pattern is useful when you have an algorithm skeleton defined in a base class and a small part of the algorithm can vary and is implemented with variations in a subclass, that is that is to say the concrete class. Essentially, the model of the algorithm is defined in the base class and while some steps of the algorithm (the part that varies those that vary from the base class) are redefined in a concrete class.

The Strip of four defines the model method model as following:

Define the skeleton of an algorithm in an operation, deferring certain steps to subclasses. The Template method allows subclasses to redefine certain steps of an algorithm without changing the structure of the algorithm.

In other words, the model method allows you to define a group of interchangeable, similarly structured, multi-step algorithms in your application. Note that each of these algorithms adheres to the same sequence of steps but has a different implementation. The base class defines the structure of the algorithm, while the derived classes define variations on the algorithm without changing its structure. As the name of the template suggests, the base class serves as a sort of template or placeholder.

The model method model in action

Let’s take a look at how this design pattern can be implemented. Typical participants in the model method model are an abstract class and a concrete class. Consider the class below, which contains two types of methods. One defines each step of an algorithm, while the other is a template method that checks the algorithm and invokes the steps.

public abstract class AlgorithmBase
    {
        public void TemplateMethod()
        {
            OperationA();
            OperationB();
            OperationC();
        }
        public abstract bool OperationA();
        public abstract bool OperationB();
        public abstract bool OperationC();
    }

As you can see, the AlgorithmBase abstract class defines a TemplateMethod and three abstract methods that perform the steps of the algorithm.

And here is the concrete class for your reference:

public sealed class AlgorithmA : AlgorithmBase
     {
         public override bool OperationA()
         {
             Console.WriteLine(“Inside OperationA() of AlgorithmA”);
             return true;
         }
          public override bool OperationB()
         {
             Console.WriteLine(“Inside OperationB() of AlgorithmA”); 
             return true;
         }
          public override bool OperationC()
         {
             Console.WriteLine(“Inside OperationC() of AlgorithmA”);
             return true;
         }
     } 

The AlgorithmA class extends the AlgorithmBase class and implements each of the abstract methods.

Likewise, you could have another concrete class that extends the AlgorithmBase class and implements each of the abstract methods in its own way. For example:

public sealed class AlgorithmB : AlgorithmBase
     {
         public override bool OperationA()
         {
             Console.WriteLine(“Inside OperationA() of AlgorithmB”);
             return true;
         }
         public override bool OperationB()
         {
             Console.WriteLine(“Inside OperationB() of AlgorithmB”);
             return true;
         }
         public override bool OperationC()
         {
             Console.WriteLine(“Inside OperationC() of AlgorithmB”);
             return true;
         }
     }

Finally, the following code snippet shows how you can call model methods on instances of the AlgorithmA and AlgorithmB classes.

static void Main(string[] args)
         {
             AlgorithmBase obj1 = new AlgorithmA();
             obj1.TemplateMethod();
             AlgorithmBase obj2 = new AlgorithmB();
             obj2.TemplateMethod();
             Console.Read();

}
The template method design template allows you to implement a general algorithm while still leaving room for easy customization. You will probably find it useful over and over again.

Copyright © 2017 IDG Communications, Inc.


Share.

Leave A Reply