Dahua Design Pattern Template Pattern C# Dahua Design Pattern: Prototype Pattern

Learning is endless, keep improving

Ten years in Hedong, ten years in Hexi, don't bully the poor

Let’s discuss the template pattern together today, as follows:

1. Concept

  The last article talked about the big talk design pattern: prototype pattern . The prototype pattern is mainly through the Clone () method " deep and shallow copy " to create new objects, eliminating the initialization process. The template method pattern will also "cut corners", move the unchanged behavior to the superclass, remove the repeated code in the subclass, and fully demonstrate the advantages of code reuse . It provides us with a specific structure and style, we only need to care about filling the data content, don't worry~

  Let's take a look at its precise definition:

  TemplateMethod pattern: Defines the skeleton of an algorithm in an operation, while deferring some steps to subclasses. Template methods allow subclasses to redefine certain steps of an algorithm without changing its structure.

 

2. UML diagram

Abstract Class (AbstractClass):

1) One or more abstract operations are defined for subclasses to implement. These abstract operations are called basic operations, and they are the constituent steps of a top-level logic. 2) A template method is defined and implemented, which is usually a detailed method, which gives the skeleton of a top-level logic, and the logical composition steps are deferred to the subclass implementation in the corresponding abstract operation.

 

Detailed class (ConcreteClass):

1) Implement one or more abstract methods defined by the parent class, which are a top-level logic in the composition step.

2) Each abstract template role can have any number of detailed template roles corresponding to it, and each detailed template role can give different implementations of these abstract methods, so that the implementation of top-level logic is different.

 

Three, code analysis

namespace TemplateMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            AbstractClass A = new ConcreteClassA();
            A.TemplateMethod();
            AbstractClass B = new ConcreteClassB();
            B.TemplateMethod();
            Console.ReadKey();
        }
    }

    public abstract class AbstractClass
    {
        public  abstract  void PrimitiveOperation1 ();
        public  abstract  void PrimitiveOperation2 ();

        public void TemplateMethod()
        {
            PrimitiveOperation1 ();
            PrimitiveOperation2 ();
            Console.WriteLine("");
        }
    }

    class ConcreteClassA : AbstractClass
    {
        public  override  void PrimitiveOperation1 ()
        {
            Console.WriteLine( " Concrete class A. Method 1 implementation " );
        }

        public  override  void PrimitiveOperation2 ()
        {
            Console.WriteLine( " Concrete class A. Method 2 implementation " );
        }
    }

    class ConcreteClassB : AbstractClass
    {
        public  override  void PrimitiveOperation1 ()
        {
            Console.WriteLine( " Concrete class B. Method 1 implementation " );
        }

        public  override  void PrimitiveOperation2 ()
        {
            Console.WriteLine( " Concrete class B. Method 2 implementation " );
        }
    }
}

Summarize:

(1) The template method pattern reflects its advantages by moving the invariant behavior to the superclass and taking out the repeated code in the subclass.

(2) Template mode is to provide a good code reuse platform.

(3) When invariant and mutable behavior are mixed in subclass implementations of methods, invariant behavior is repeated in subclasses. We move these behaviors to a single place through the template method pattern, which helps subclasses get rid of the entanglement of repeated invariant behaviors.

@chenwolong's blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024574&siteId=291194637