creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

Programming - Abstract Factory Design Pattern


Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
The classes and/or objects participating in the example are:


  • AbstractFactory
    • declares an interface for operations that create abstract products
  • ConcreteFactoryand
    • implements the operations to create concrete product objects
  • AbstractProduct
    • declares an interface for a type of product object
  • Productand
    • defines a product object to be created by the corresponding concrete factory
    • implements the AbstractProduct interface
  • Clientand
    • uses interfaces declared by AbstractFactory and AbstractProduct classes
// Abstract Factory pattern -- Structural example

using
System;

namespace DoFactory.GangOfFour.Abstract.Structural
{
// MainApp test application

class MainApp
{
public static void Main()
{
// Abstract factory #1
AbstractFactory factory1 = new ConcreteFactory1();
Client c1 = new Client(factory1);
c1.Run();

// Abstract factory #2
AbstractFactory factory2 = new ConcreteFactory2();
Client c2 = new Client(factory2);
c2.Run();

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

// "AbstractFactory"

abstract class AbstractFactory
{
public abstract AbstractProductA CreateProductA();
public abstract AbstractProductB CreateProductB();
}

// "ConcreteFactory1"

class ConcreteFactory1 : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA1();
}
public override AbstractProductB CreateProductB()
{
return new ProductB1();
}
}

// "ConcreteFactory2"

class ConcreteFactory2 : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA2();
}
public override AbstractProductB CreateProductB()
{
return new ProductB2();
}
}

// "AbstractProductA"

abstract class AbstractProductA
{
}

// "AbstractProductB"

abstract class AbstractProductB
{
public abstract void Interact(AbstractProductA a);
}

// "ProductA1"

class ProductA1 : AbstractProductA
{
}

// "ProductB1"

class ProductB1 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name +
" interacts with " + a.GetType().Name);
}
}

// "ProductA2"

class ProductA2 : AbstractProductA
{
}

// "ProductB2"

class ProductB2 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name +
" interacts with " + a.GetType().Name);
}
}

// "Client" - the interaction environment of the products

class Client
{
private AbstractProductA AbstractProductA;
private AbstractProductB AbstractProductB;

// Constructor
public Client(AbstractFactory factory)
{
AbstractProductB = factory.CreateProductB();
AbstractProductA = factory.CreateProductA();
}

public void Run()
{
AbstractProductB.Interact(AbstractProductA);
}
}
}
and
Output to be expected:
and
ProductB1 interacts with ProductA1
ProductB2 interacts with ProductA2



Other pages of interest:
[+] Consume .NET services without Silverlight
[+] Case studies - Software in business
[+] Software languages and development platforms
[+] Embedded Systems Engineering Embedded software and embedded systems, are those that require 100% o..
[+] Enterprise Java performance tuning tips
[+] Microsoft Announces New Support for Silverlight by Content Companies Worldwide
[+] Forward thinking eCommerce is the next-generation dot com Forward thinking eCommerce is about pr..
[+] Java (J2EE) provides a scalable and robust framework for Enterprise application development Java ..
[+] Embedded Systems Engineering Embedded software and embedded systems, are those that require 100% o..
[+] Programming - Design patterns in Software
[+] More news on Java OpenSource initiative
[+] Enterprise Java performance tuning tips
[+] Microsoft is looking to acquire forward thinking search engine technoogy trovider Powerset


Comments(0)

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: