Factory Method pattern in C#

This code shows how to use factory method pattern in C#


using System;
using System.Collections;

namespace DoFactory.GangOfFour.Factory.Structural
{


class MainApp
{
static void Main()
{

Creator[] creators = new Creator[2];
creators[0] = new ConcreteCreatorX();
creators[1] = new ConcreteCreatorY();

foreach(Creator creator in creators)
{
Product product = creator.FactoryMethod();
Console.WriteLine("Created {0}",
product.GetType().Name);
}

Console.Read();
}
}


abstract class Product
{
}



class ConcreteProductX : Product
{
}


class ConcreteProductY : Product
{
}


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


class ConcreteCreatorX : Creator
{
public override Product FactoryMethod()
{
return new ConcreteProductX();
}
}


class ConcreteCreatorY : Creator
{
public override Product FactoryMethod()
{
return new ConcreteProductY();
}
}
}


Comments

Author: Happy Coder12 Nov 2008 Member Level: Gold   Points : 2

Another Sample

    using System;

// These two classes could be part of a framework,
// which we will call DP
// ===============================================

class DPDocument
{


}

abstract class DPApplication
{
protected DPDocument doc;

abstract public void CreateDocument();

public void ConstructObjects()
{

// Create objects as needed
// . . .

// including document
CreateDocument();
}
abstract public void Dump();
}

// These two classes could be part of an application
// =================================================

class MyApplication : DPApplication
{
override public void CreateDocument()
{
doc = new MyDocument();
}

override public void Dump()
{
Console.WriteLine("MyApplication exists");
}
}

class MyDocument : DPDocument
{

}

///
/// Summary description for Client.
///

public class Client
{
public static int Main(string[] args)
{
MyApplication myApplication = new MyApplication();
myApplication.ConstructObjects();
myApplication.Dump();
return 0;
}
}

Author: Happy Coder12 Nov 2008 Member Level: Gold   Points : 2


You should consider using a Factory pattern when
· A class can’t anticipate which kind of class of objects it must create.
· A class uses its subclasses to specify which objects it creates.
· You want to localize the knowledge of which class gets created.

There are several similar variations on the factory pattern to recognize.
1. The base class is abstract and the pattern must return a complete working class.
2. The base class contains default methods and is only subclassed for cases where the default methods are insufficient.
3. Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different.

Happy Coder



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: