SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet.
SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet.
SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet.
SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet.
Software Outsourcing Ukraine : SoftPilot.2000 - Site Map SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet.
SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Factory Method. Creational Patterns. dotNet.
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 from Ukraine.

Software development articles about design patterns implementation in C#.

Creational Patterns.




Factory Method.

Actually useful design pattern. Let's imagine - you are in development of some B2B application and you need to keep working with two (or more) different databases, which have different APIs. It is the real situation. For example, you have the SQLite RDBMS at the client side and something more heavy at the server. Let it be the PostgreSQL. Frankly speaking, we actually interested in one interface for work with different RDBMS. Factory Method will help us.

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.

Let's try to implement this in C#.

 

 

   
  Factory Method Pattern, C# implementation:

//Factory Method design pattern implementation in C#

//------------------------------------
// The DB Interface Implementation

using System;
public enum TypeDB {SQLite, PostgreSQL};

interface DB
{
  void Open();
  void Close();
}
//------------------------------------------
// 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 database processing concrete realization classes

class PostgreDB : DB
{
  public void Open() { /* PostgreDB implementation here*/ }
  public void Close() { /* PostgreDB implementation here*/ }
}

class SQLiteDB : DB
{
  public void Open() { /* SQLiteDB implementation here*/ }
  public void Close() { /* SQLiteDB implementation here*/ }
}

//------------------------------------------
// The client class implementation

class MyApplicationClass
{
  public static void Main()
{

  FabricDB fd = new FabricDB();
  DB postgreDB = fd.GetDB( TypeDB.PostgreSQL );
  DB sqliteDB = fd.GetDB( TypeDB.SQLite );

   sqliteDB.Open();
   /*.. work with SQLite here ..*/
   sqliteDB.Close();

   postgreDB.Open();
   /*.. work with PostgreSQL here ..*/
   postgreDB.Close();

}
}

Design Patterns. Factory Method. C#.



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