1. Template method Mode
Prepare an abstract class, implement part of the logic in the form of concrete methods and concrete constructs, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the purpose of the template method pattern.
Many people may not think that the template method pattern is actually one of the most common patterns in all patterns, and that many people may have used the template method pattern without realizing that they have used the pattern. Template method pattern is based on the basic technology of inheritance code reuse, the structure and usage of template method pattern is also the core of object-oriented design.
Template method patterns require the development of collaboration between abstract classes and designers of specific subclasses. A designer is responsible for the outline and skeleton of an algorithm, while others are responsible for the logical steps of the algorithm. The methods that represent these specific logical steps are called the basic methods (primitive method), and the method of combining these basic methods is called the template approach (template method), which is the name of the design pattern.
2. Template method Pattern Structure
There are two roles involved:
The abstract template (abstractclass) role has the responsibility to define one or more abstract operations so that subclasses can be implemented. These abstract operations are called basic operations, and they are the constituent steps of a top-level logic. Defines and implements a template method. This template method is generally a concrete method, which gives the skeleton of a top-level logic, and the logical composing step is deferred to the subclass implementation in the corresponding abstract operation. It is also possible for top-level logic to invoke some specific methods.
The specific template (Concreteclass) role has the responsibility to implement one or more abstract methods defined by the parent class, which are the constituent steps of a top-level logic. Each abstract template role can have any number of specific template roles to correspond to, and each specific template role can give the different implementations of these abstract methods (i.e., the constituent steps of the top-level logic) so that the implementations of the top-level logic vary.
3. The method in Template method mode
The methods in the template method can be divided into two main categories: the Template Method (Template methods) and the basic method (primitive).
Stencil Method
A template method is defined in an abstract class, combining the basic operation method together to form a total algorithm or a method of total behavior. This template method is generally defined in an abstract class and is completely inherited by subclasses without modification.
Basic Methods
The basic methods can be divided into three kinds: abstract methods (abstraction method), specific methods (concrete method) and hook method (hook methods).
1 Abstract Method: An abstract method is declared by an abstract class and implemented by a specific subclass. An abstract method in the C # language is marked with the abstract keyword.
2 The Concrete method: A concrete method is declared and implemented by an abstract class, and the subclass is not implemented or displaced. In the C # language, a specific method does not have an abstract keyword.
3 Hook method: A hook method is declared and implemented by an abstract class, and subclasses are extended. Typically, an implementation given by an abstract class is an empty implementation, as the default implementation of a method. (Projects created by the Project Wizard in Visual FoxPro use a Apphook class to implement the task of monitoring project member changes and adjusting the system structure.) The name of the hook method is usually started with do.
4. Template method Pattern Sample using system; using system.data; Template Method Abstract class Public abstract class abstractmethod { // basic method public abstract bool connectdatabase (); // Basic Method public abstract dataset getdata (); // Basic Method public abstract int execquerynon (); // Basic Methods public abstract void Disconnectdatabase (); // Template Method public void process () { if (Connectdatabase () == true) { int ss = execquerynon (); if (ss > 0) { Dataset ds = getdata (); } disconnectdatabase ();        &NBSP}  }}// Template method abstract subclass Public class concretemethod : abstractmethod { public override bool Connectdatabase () { return true;    &NBSP} public override dataset getdata () {       &NBSp;return new dataset ();    &NBSP} public override int execquerynon () { return 11; } public override void disconnectdatabase () { return;    &NBSP}}// application instance Public static class main { public void dosearch () { // Instantiate an abstract subclass, inheriting the base class template method "process ()" concretemethod method = new concretemethod (); method. Process ();    &NBSP}}