Factory and Abstract Factory patterns

Posted: December 1, 2010 in Design Patterns

Factory and Abstract Factory patterns are creational patterns.

Factory pattern – “Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.” This is a loosely coupled model as it returns an instance of one of several possible classes, depending on the data provided to it.The calling object can simply request the factory to return the object it needs, without actually knowing how to create that object i.e. through interface.In order to add or remove a new module, one only needs to modify the decision code in the factory.

Abstract factory – Provide an interface for creating families of related or dependent objects without specifying their concrete classes. This is one more level of abstraction above the factory pattern. Abstract factory is a factory object that returns one of the several factories.

Code snippet for both patterns is as seen below :

——————————————————————————————————————————————
Factory pattern sample

using System;

namespace DesignPatterns
{
// Client invoking class
public class Client
{
public static void Main()
{
IConnection connection1 = ConnectionFactory.GetConnection(ConnectionType.Sql);
connection1.Connect();

IConnection connection2 = ConnectionFactory.GetConnection(ConnectionType.Oracle);
connection2.Connect();

Console.ReadLine();
}
}

public enum ConnectionType
{
Sql = 0,
Oracle = 1

// Add new Providers
// Provider = 2….
} ;

// Factory class to create objects at runtime based on user selection
public class ConnectionFactory
{

public static IConnection GetConnection(ConnectionType connType)
{
if (connType == ConnectionType.Oracle)
return new OracleConnection();

// need to add more cases if our Connection providers are more
// else if(connType == )
//return new Provider();

// default
return new SqlConnection();
}
}

// Common factory interface for interacting with classes at runtime
public interface IConnection
{
void Connect();
void DisConnect();
}

public class SqlConnection : IConnection
{

#region IConnection Members

public void Connect()
{
Console.WriteLine(“Connected to Sql”);
}

public void DisConnect()
{
Console.WriteLine(“Disconnected from Sql”);
}

#endregion
}

public class OracleConnection : IConnection
{

#region IConnection Members

public void Connect()
{
Console.WriteLine(“Connected to Oracle”);
}

public void DisConnect()
{
Console.WriteLine(“Disconnected from Oracle”);
}

#endregion
}

}

——————————————————————————————————————————————
Abstract Factory Pattern

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace DesignPatterns
{
// Client invoking class
public class Client
{
public static void Main()
{
IBreadFactory indianBreadfactory = new IndianBreadFactory();
IndianVegBread indianVegBread = indianBreadfactory.GetBread(0) as IndianVegBread;
IndianNonVegBread indianNonVegBread = indianBreadfactory.GetBread(1) as IndianNonVegBread;

IBreadFactory americanBreadFactory = new AmericanBreadFactory();
AmericanPlainBread plainBread = americanBreadFactory.GetBread(0) as AmericanPlainBread;
AmericanBreadWithCheese cheeseBread = americanBreadFactory.GetBread(1) as AmericanBreadWithCheese;

Console.ReadLine();
}
}

// Common factory interface for interacting with classes at runtime
public interface IBread
{
void Bake();
}

public interface IBreadFactory
{
IBread GetBread(int i);
}

// First Factory
public class IndianBreadFactory : IBreadFactory
{
public IBread GetBread(int i)
{
IBread bread;
if (i == 0)
bread = new IndianVegBread();

bread = new IndianNonVegBread();
return bread;
}
}

// Second Factory
public class AmericanBreadFactory : IBreadFactory
{
public IBread GetBread(int i)
{
IBread bread;
if (i == 0)
bread = new AmericanPlainBread();

bread = new AmericanBreadWithCheese();
return bread;
}
}

// American Bread products
public class AmericanPlainBread : IBread
{
public void Bake()
{
Console.WriteLine(“American Plain Bread baked”);
}
}

public class AmericanBreadWithCheese : IBread
{
public void Bake()
{
Console.WriteLine(“American Bread With Cheese baked”);
}
}

// Indian Bread Products
public class IndianVegBread : IBread
{
public void Bake()
{
Console.WriteLine(“Indian Veg Bread baked”);
}
}

public class IndianNonVegBread : IBread
{
public void Bake()
{
Console.WriteLine(“Indian Non Veg Bread baked”);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s