Showing posts with label abstract facotory pattern. Show all posts
Showing posts with label abstract facotory pattern. Show all posts

Friday, May 7, 2010

Difference between factory and abstract factory pattern


Abstract Factory:- Creates an instance of several families of classes. we are using an interface to create a family of related or dependant classes without specifying their concrete classes.
abstract factory uses factory method to create the object of familites class.
abstract factory pattern focus on the delegation of the instantiation to another object

The classes and/or objects participating in this pattern are:
AbstractFactory (interface)
declares an interface for operations that create abstract products.

ex.- ContinentFactory
ConcreteFactory (class)
implements the operations to create concrete product objects.

basically it the object of various concrete product and it will return the type of abstractProduct.
ex.- AfricaFactory, AmericaFactory
AbstractProduct (interface)
declares an interface for a type of product object. basically it contain the definition of product or we can say it is the template of product.

ex.- Herbivore, Carnivore
Product ( class)
defines a product object to be created by the corresponding concrete factory
implements the AbstractProduct interface.

it is a kind of implementation of template product.
ex.- Wildebeest, Lion, Bison, Wolf
Client (class)
uses interfaces declared by AbstractFactory and AbstractProduct classes

it's a real user of the patterns.
ex.- AnimalWorld


// Abstract Factory pattern -- Real World example
using System;

namespace DoFactory.GangOfFour.Abstract.RealWorld
{
///


/// MainApp startup class for Real-World
/// Abstract Factory Design Pattern.
///

class MainApp
{
///
/// Entry point into console application.
///

public static void Main()
{
// Create and run the African animal world
ContinentFactory africa = new AfricaFactory();
AnimalWorld world = new AnimalWorld(africa);
world.RunFoodChain();

// Create and run the American animal world
ContinentFactory america = new AmericaFactory();
world = new AnimalWorld(america);
world.RunFoodChain();

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


///
/// The 'AbstractFactory' abstract class
///

abstract class ContinentFactory
{
public abstract Herbivore CreateHerbivore();
public abstract Carnivore CreateCarnivore();
}

///
/// The 'ConcreteFactory1' class
///

class AfricaFactory : ContinentFactory
{
public override Herbivore CreateHerbivore()
{
return new Wildebeest();
}
public override Carnivore CreateCarnivore()
{
return new Lion();
}
}

///
/// The 'ConcreteFactory2' class
///

class AmericaFactory : ContinentFactory
{
public override Herbivore CreateHerbivore()
{
return new Bison();
}
public override Carnivore CreateCarnivore()
{
return new Wolf();
}
}

///
/// The 'AbstractProductA' abstract class
///

abstract class Herbivore
{
}

///
/// The 'AbstractProductB' abstract class
///

abstract class Carnivore
{
public abstract void Eat(Herbivore h);
}

///
/// The 'ProductA1' class
///

class Wildebeest : Herbivore
{
}

///
/// The 'ProductB1' class
///

class Lion : Carnivore
{
public override void Eat(Herbivore h)
{
// Eat Wildebeest
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}

///
/// The 'ProductA2' class
///

class Bison : Herbivore
{
}

///
/// The 'ProductB2' class
///

class Wolf : Carnivore
{
public override void Eat(Herbivore h)
{
// Eat Bison
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}

///
/// The 'Client' class
///

class AnimalWorld
{
private Herbivore _herbivore;
private Carnivore _carnivore;

// Constructor
public AnimalWorld(ContinentFactory factory)
{
_carnivore = factory.CreateCarnivore();
_herbivore = factory.CreateHerbivore();
}

public void RunFoodChain()
{
_carnivore.Eat(_herbivore);
}
}
}


Factory Method :- it Creates an instance of several derived classes (concrete class)
this pattern Defines an interface for creating An Object but let sub class decide which class to instantiate.
factory method/pattern uses inheritance to decide which object has to be instantiated.
it
focus on the use of inheritance to decide the object to be instantiated.
factories means " to decouple the creation of an object from their code"

The classes and/or objects participating in this pattern are:
Product
(Page)
defines the interface of objects the factory method creates
ConcreteProduct (SkillsPage, EducationPage, ExperiencePage)
implements the Product interface

Creator (Document)
declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create a Product object.

ConcreteCreator (Report, Resume)
overrides the factory method to return an instance of a ConcreteProduct.
Code :-


// Factory Method pattern -- Structural example
using System;

namespace DoFactory.GangOfFour.Factory.Structural
{
///
/// MainApp startup class for Structural
/// Factory Method Design Pattern.
///

class MainApp
{
///
/// Entry point into console application.
///

static void Main()
{
// An array of creators
Creator[] creators = new Creator[2];

creators[0] = new ConcreteCreatorA();
creators[1] = new ConcreteCreatorB();

// Iterate over creators and create products
foreach (Creator creator in creators)
{
Product product = creator.FactoryMethod();
Console.WriteLine("Created {0}",
product.GetType().Name);
}

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

///
/// The 'Product' abstract class
///

abstract class Product
{
}

///
/// A 'ConcreteProduct' class
///

class ConcreteProductA : Product
{
}

///
/// A 'ConcreteProduct' class
///

class ConcreteProductB : Product
{
}

///
/// The 'Creator' abstract class
///

abstract class Creator
{
public abstract Product FactoryMethod();
}

///
/// A 'ConcreteCreator' class
///

class ConcreteCreatorA : Creator
{
public override Product FactoryMethod()
{
return new ConcreteProductA();
}
}

///
/// A 'ConcreteCreator' class
///

class ConcreteCreatorB : Creator
{
public override Product FactoryMethod()
{
return new ConcreteProductB();
}
}
}


more information download the below links :-
http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf
http://www.dofactory.com/Patterns/PatternFactory.aspx
http://www.dofactory.com/Patterns/PatternAbstract.aspx