Object Oriented Programming Patterns for Geeks
A Simple Factory
The basic idea to factories is that they create objects for you.
The simplest example is given here:
namespace ooppatterns
{
using System;
using System.Collections.Generic;
internal class Program
{
private static void Main( string[] args )
{
List<IDoSomething> aList = new List<IDoSomething>();
SimpleMethodFactory factory = new SimpleMethodFactory();
for ( int i = 0; i < 32; i++ )
aList.Add( factory.Create( i ) );
foreach ( IDoSomething ID in aList )
Console.WriteLine( ID.DoSomething );
Console.ReadLine();
}
}
// product
interface IDoSomething
{
string DoSomething { get; }
}
// Concrete Creator
class SimpleMethodFactory
{
/// <summary>
/// Manufactures and object based on criteria.
/// </summary>
/// <param name="manufactureCriteria">The manufacture criteria.</param>
/// <returns>An IDoSomething derived object</returns>
public IDoSomething Create( int manufactureCriteria )
{
if ( manufactureCriteria % 15 == 0 )
return new Example01( "This is created using MOD 15 FIZZBANG" );
if ( manufactureCriteria % 5 == 0 )
return new Example02( "This is created using MOD 5 FIZZ" );
if ( manufactureCriteria % 3 == 0 )
return new Example03( "This is created using MOD 3 BANG" );
return new Example00( manufactureCriteria.ToString() );
}
}
// Concrete Product 00
class Example00 : IDoSomething
{
private string _value;
public Example00( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example00 : {0}", this._value ); }
}
}
// Concrete Product 01
class Example01 : IDoSomething
{
private string _value;
public Example01( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example01 : {0}", this._value ); }
}
}
// Concrete Product 02
class Example02 : IDoSomething
{
private string _value;
public Example02( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example02 : {0}", this._value ); }
}
}
// Concrete Product 03
class Example03 : IDoSomething
{
private string _value;
public Example03( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example03 : {0}", this._value ); }
}
}
}
Another take for GoF purists:
namespace ooppatterns
{
using System;
using System.Collections.Generic;
internal class Program
{
private static void Main( string[] args )
{
SimpleMethodFactoryBase[] factories =
{
new SimpleMethodFactoryA(),
new SimpleMethodFactoryB()
};
List<IDoSomething> aList = new List<IDoSomething>();
for ( int i = 0; i < 32; i++ )
aList.Add( factories[1-(i % 2)].Create( i ) );
foreach ( IDoSomething ID in aList )
Console.WriteLine( ID.DoSomething );
Console.ReadLine();
}
}
// Product
interface IDoSomething
{
string DoSomething { get; }
}
// Creator
abstract class SimpleMethodFactoryBase
{
/// <summary>
/// Manufactures and object based on criteria.
/// </summary>
/// <param name="manufactureCriteria">The manufacture criteria.</param>
/// <returns>An IDoSomething derived object</returns>
public abstract IDoSomething Create( int manufactureCriteria );
}
// Concrete Creator A
class SimpleMethodFactoryA : SimpleMethodFactoryBase
{
public override IDoSomething Create( int manufactureCriteria )
{
if ( manufactureCriteria % 15 == 0 )
return new Example01( "This is created using MOD 15 FIZZBANG" );
if ( manufactureCriteria % 5 == 0 )
return new Example02( "This is created using MOD 5 FIZZ" );
if ( manufactureCriteria % 3 == 0 )
return new Example03( "This is created using MOD 3 BANG" );
return new Example00( manufactureCriteria.ToString() );
}
}
// Concrete Creator B
class SimpleMethodFactoryB : SimpleMethodFactoryBase
{
public override IDoSomething Create( int manufactureCriteria )
{
if ( manufactureCriteria % 13 == 0 )
return new Example03( "This is created using MOD 13 Wibble" );
if ( manufactureCriteria % 10 == 0 )
return new Example01( "This is created using MOD 10 Wobble" );
if ( manufactureCriteria % 7 == 0 )
return new Example02( "This is created using MOD 7 Wubble" );
return new Example00( manufactureCriteria.ToString() );
}
}
// Concrete Product 00
class Example00 : IDoSomething
{
private string _value;
public Example00( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example00 : {0}", this._value ); }
}
}
// Concrete Product 01
class Example01 : IDoSomething
{
private string _value;
public Example01( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example01 : {0}", this._value ); }
}
}
// Concrete Product 02
class Example02 : IDoSomething
{
private string _value;
public Example02( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example02 : {0}", this._value ); }
}
}
// Concrete Product 03
class Example03 : IDoSomething
{
private string _value;
public Example03( string value )
{
this._value = value;
}
public string DoSomething
{
get { return string.Format( "Example03 : {0}", this._value ); }
}
}
}