• Türkçe (Türkiye)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • Oturum açma
  • Kayıt ol
DotNetAge - Mvc & jQuery CMS
Kenar çubuğunu gizle

Abstract Factory


Definition



Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

UML class diagram





Participants



The classes and/or objects participating in this pattern are:
  • AbstractFactory - declares an interface for operations that create abstract products
  • ConcreteFactory - implements the operations to create concrete product objects
  • AbstractProduct - declares an interface for a type of product object
  • Product
defines a product object to be created by the corresponding concrete factory

implements the AbstractProduct interface

  • Client - uses interfaces declared by AbstractFactory and AbstractProduct classes

Sample code in C#




///
/// MainApp startup class for Structural
/// Abstract Factory Design Pattern.
///
class MainApp
{
///
/// Entry point into console application.
///
public static void Main()
{
// Abstract factory #1
AbstractFactory factory1 = new ConcreteFactory1();
Client client1 = new Client(factory1);
client1.Run();


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


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



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

public abstract AbstractProductA CreateProductA();

public abstract AbstractProductB CreateProductB();

}



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

public class ConcreteFactory1 : AbstractFactory
{

public override AbstractProductA CreateProductA()
{
return new ProductA1();
}

public override AbstractProductB CreateProductB()
{
return new ProductB1();
}
}



///
/// The 'ConcreteFactory2' class
///
public class ConcreteFactory2 : AbstractFactory
{

public override AbstractProductA CreateProductA()
{
return new ProductA2();
}

public override AbstractProductB CreateProductB()
{
return new ProductB2();
}
}


///
/// The 'AbstractProductA' abstract class
///
public abstract class AbstractProductA
{
}



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


///
/// The 'ProductA1' class
///
public class ProductA1 : AbstractProductA
{
}



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

///
/// The 'ProductA2' class
///
public class ProductA2 : AbstractProductA
{
}


///
/// The 'ProductB2' class
///
public class ProductB2 : AbstractProductB
{

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



///
/// The 'Client' class. Interaction environment for the products.
///
public 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);
}
}

 


    Average:
  • Okur
    (780)
  • (0)
  • Permalink
Paylaşmak için:

Yorumlar (0)


  • rss
  • atom

Bu makalede bulunan herhangi bir yorum var demektir.

Tag cloud

Anything in here will be replaced on browsers that support the canvas element