ArticleDirectory
- Definition
- Instance 1-database connection Template
- Strengths and weaknesses
- Application scenarios
Defining an operationAlgorithmTo delay the implementation of some steps in the subclass. The template method allows the subclass to redefine some steps of an algorithm without changing the algorithm structure.
The UML class diagram is as follows:
The relationship between classes and objects is:
1. abstractclass (abstract class): defines an abstract original operation, and its subclass can redefine it to implement each step of an algorithm; implement a template method to define the skeleton of an algorithm, this template method can not only call the original operation, but also call methods defined in abstractclass or other objects.
2. concreteclass (specific subclass): implements the original operation to complete the steps of the subclass-specific algorithm.
The following figure shows the order of typical applications:
- Instance 1-database connection Template
The followingCodeDemonstrate the template method. Run () provides a skeleton for calling the method sequence. The connect, select, process, and disconnect methods are delayed in the merdataobject subclass. The class diagram is as follows:
View code
Namespace Leleapplication4
// Abstractclass abstract class
Abstract Class Dataobject
{
// Methods Method
Abstract Public Void Connect ();
Abstract Public Void Select ();
Abstract Public Void Process ();
Abstract Public Void Disconnect ();
// The template method Template Method
Public Void Run ()
{
Connect ();
Select ();
Process ();
Disconnect ();
}
}
// Concreteclass
Class Customerdataobject: dataobject
{
Private String Connectionstring = " Server =. uid = sa; Pwd = sa; database = northwind; Connection Reset = false; " ;
Private String Commandstring;
Private Dataset dataset;
Private Sqlconnection dataconnection;
// Methods Method
Public Override Void Connect ()
{
Dataconnection =New Sqlconnection (connectionstring );
Dataconnection. open ();
}
Public Override Void Select ()
{
Commandstring = " Select top 5 companyName from MERs " ;
Sqldataadapter dataadapter = New Sqldataadapter (commandstring, dataconnection );
Dataset =New Dataset ();
Dataadapter. Fill (dataset, " MERs " );
}
Public Override Void Process ()
{
Datatable = dataset. Tables [ " MERs " ];
Foreach (Datarow In Datatable. Rows)
Console. writeline (datarow [ " CompanyName " ]);
}
Public Override Void Disconnect ()
{
Dataset. Dispose ();
Dataconnection. Close ();
}
}
Class Program
{
Static Void Main ( String [] ARGs)
{
Customerdataobject c = New Customerdataobject ();
C. Run ();
Console. Read ();
}
}
}
The template method mode formally defines an algorithm in a class, and its sub-classes implement detailed processing. The advantage of the template method mode is that the structure of the algorithm is not changed when the subclass defines the processing algorithm.
The template method is characterized by defining a subclass for each different implementation, which also combines the High Cohesion responsibility allocation mode and cannot be said to be its disadvantage.
The following scenario is suitable for the template method mode:
1. You want to put the same algorithm in a class and put the changed part of the algorithm in the subclass for implementation.
2. Subclass public algorithms should be placed in a public class to avoid code duplication.