• Deutsch (Deutschland)
    • English (United States)
    • العربية (مصر)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • Anmeldung
  • Registrieren
DotNetAge - Mvc & jQuery CMS
Seitenleiste ausblenden

Template Method


Definition



Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

UML class diagram



templatemethod

Participants



The classes and/or objects participating in this pattern are:
  • AbstractClass
defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm

implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
  • ConcreteClass - implements the primitive operations ot carry out subclass-specific steps of the algorithm


Sample code in C#




///
/// MainApp startup class for Real-World
/// Template Design Pattern.
///
class MainApp
{
///
/// Entry point into console application.
///
static void Main()
{
DataAccessObject daoCategories = new Categories();
daoCategories.Run();

DataAccessObject daoProducts = new Products();
daoProducts.Run();

// Wait for user
Console.ReadKey();
}
}

///
/// The 'AbstractClass' abstract class
///
abstract class DataAccessObject
{
protected string connectionString;
protected DataSet dataSet;



public virtual void Connect()
{
// Make sure mdb is available to app
connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source=..\\..\\..\\db1.mdb";

}



public abstract void Select();

public abstract void Process();



public virtual void Disconnect()
{
connectionString = "";
}


// The 'Template Method'
public void Run()
{
Connect();

Select();

Process();

Disconnect();

}
}



///
/// A 'ConcreteClass' class
///
class Categories : DataAccessObject
{
public override void Select()
{
string sql = "select CategoryName from Categories";

OleDbDataAdapter dataAdapter = new OleDbDataAdapter( sql, connectionString);

dataSet = new DataSet();

dataAdapter.Fill(dataSet, "Categories");

}



public override void Process()
{

Console.WriteLine("Categories -- ");

DataTable dataTable = dataSet.Tables["Categories"];

foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["CategoryName"]);
}
Console.WriteLine();
}
}


///
/// A 'ConcreteClass' class
///
class Products : DataAccessObject
{
public override void Select()
{
string sql = "select ProductName from Products";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( sql, connectionString);
dataSet = new DataSet();
dataAdapter.Fill(dataSet, "Products");
}


public override void Process()
{
Console.WriteLine("Products -- "
);
DataTable dataTable = dataSet.Tables["Products"];
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["ProductName"]);
}
Console.WriteLine();
}
}

 


    Average:
  • Liest
    (1901)
  • Permalink
Zu teilen:

Tag cloud

Anything in here will be replaced on browsers that support the canvas element