Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method
Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method
Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method
Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method
softPilot.2000 : Software Outsourcing to Ukraine - Site Map Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method
Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Creational Patterns. Behavioral Patterns. Factory Method. Template Method
SoftPilot.2000 : Software Outsourcing Ukraine- Home SoftPilot.2000 : Software Outsourcing Ukraine - About SoftPilot.2000 : Software Outsourcing Ukraine - Company Profile SoftPilot.2000 : Software Outsourcing Ukraine - Case Study SoftPilot.2000 : Software Outsourcing Ukraine - Services SoftPilot.2000 : Software Outsourcing Ukraine - Partners SoftPilot.2000 : Software Outsourcing Ukraine - Contacts SoftPilot.2000 : Software Outsourcing Ukraine - E-mail SoftPilot.2000 : Software Outsourcing to Ukraine

SoftPilot.2000 - Software Outsourcing to Ukraine.

Software development articles about design patterns implementation in C#.

Behavioral Patterns. Creational Patterns.




Template Method (Behavioral Design Patterns) in cooperation with Factory Method (Creational Patterns).

At one of the previous articles we were talking about Factory Method design pattern implementation.

" Implementation is simple. In general, we will have one interface ( DB), which will declare the union API for any database processing. Of course, we will have one fabric class ( FabricDB ), which will be responsible for instantiating of concrete database class. And, finally, we will have the realization of each concrete database classes ( PostgreDB and SQLiteDB ) which are derived from the DB interface." - it was the short description of the Factory Method Design Pattern implementation.

Let's imagine : we have an algorithm, which needs, between others, to process the different databases. The main skeleton of the algorithm is constant, but we have the database - dependent parts. In our example the TemplateMethodAbstractClass.ProcessDatabaseTemplateMethod() represent the skeleton of the algorithm, but the Open() and Close() part of them are implemented at the concrete database classes and different for SQLite database and PostgeSQL database.

Let's try to implement this in C#.

 

   
  Factory Method Design Pattern
Template Method Design Pattern
C# implementation:

using System;
//------------------------------------
// Factory Method (Creational Pattern) and
// Template Method (Behavioral Pattern)
// design patterns implementation in C#
//------------------------------------
// Factory Method: The DB Interface Implementation
public enum TypeDB {SQLite, PostgreSQL};

interface DB
{
void ProcessDatabaseTemplateMethod();
}

//------------------------------------------
// Template Method, abstract parent:
abstract class TemplateMethodAbstractClass
{
// abstract methods
  abstract public void Open();
  abstract public void Close();
// The Template method
public void ProcessDatabaseTemplateMethod()
{
  Console.WriteLine(" TemplateMethodAbstractClass.TemplateMethod()");
   Open();
   Close();
}
}
//------------------------------------------
// Factory Method: The database processing
// concrete realization classes
class PostgreDB : TemplateMethodAbstractClass, DB
{
  public override void Open() { Console.WriteLine("PostgreDB.Open()"); }
  public override void Close() { Console.WriteLine("PostgreDB.Close()"); }
}
class SQLiteDB : TemplateMethodAbstractClass, DB
{
  public override void Open() { Console.WriteLine("SQLiteDB.Open()"); }
  public override void Close() { Console.WriteLine("SQLiteDB.Close()"); }
}
//------------------------------------------
// Factory Method: The Fabric class implementation
class FabricDB
{
public DB GetDB (TypeDB typeDB)
{
  DB ret = null;
  switch ( typeDB )
  {
    case TypeDB.SQLite : ret = new SQLiteDB(); break;
    case TypeDB.PostgreSQL : ret = new PostgreDB(); break;
    default : break;
  }
return ret;
}
}
//------------------------------------------
// The client class implementation
  class TemplateMethodClass
{
  public static void Main()
{
// Factory method:
  FabricDB fd = new FabricDB();
  DB postgreDB = fd.GetDB( TypeDB.PostgreSQL );
  DB sqliteDB = fd.GetDB( TypeDB.SQLite );
   sqliteDB.ProcessDatabaseTemplateMethod();
  postgreDB.ProcessDatabaseTemplateMethod();
   Console.Read();
  }
}


Design Patterns. Factory Method. C#.


Design Patterns. Template Method. C#.


The results are below:


TemplateMethodAbstractClass.TemplateMethod()
SQLiteDB.Open()
SQLiteDB.Close()
TemplateMethodAbstractClass.TemplateMethod()
PostgreDB.Open()
PostgreDB.Close()



 
   
(C) SoftPilot.2000 All Rights Reserved. 2000 - 2006