25. behavioral design pattern-template method pattern (template method pattern)

Source: Internet
Author: User
ArticleDirectory
    • Definition
    • Instance 1-database connection Template
    • Strengths and weaknesses
    • Application scenarios
    • Definition

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 ();
}
}
}
    • Strengths and weaknesses

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.

    • Application scenarios

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.