'Structural' 'Real-World': Creational Patterns
'Structural' 'Real-World': Creational Patterns
world application development. Patterns are about design and interaction of objects, as well as providing
a communication platform concerning elegant, reusable solutions to commonly encountered programming
challenges.
The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are
categorized in three groups: Creational, Structural, and Behavioral. Here you will find information on these
important patterns.
To give you a head start, the C# source code is provided in 2 forms: 'structural' and 'real-world'. Structural
code uses type names as defined in the pattern definition and UML diagrams. Real-world code provides
real-world programming situations where you may use these patterns.
A third form, '.NET optimized' demonstrates design patterns that exploit built-in .NET 2.0, 3.0, and 3.5
features, such as, generics, attributes, delegates, object and collection initializers, automatic properties,
and reflection. These and much more are available in our Design Pattern Framework 3.5TM. See our
Singleton page for a .NET 3.5 Optimized code sample.
Creational Patterns
Structural Patterns
Abstract Factory
Provide an interface for creating families of related or dependent objects without specifying their concrete
classes.
Sample code in C#
This structural code demonstrates the Abstract Factory pattern creating parallel hierarchies of objects.
Object creation has been abstracted and there is no need for hard-coded class names in the client code.
// Abstract Factory pattern -- Structural example
using System;
namespace DoFactory.GangOfFour.Abstract.Structural
/// <summary>
/// </summary>
class MainApp
/// <summary>
/// Entry point into console application.
/// </summary>
// Abstract factory #1
client1.Run();
// Abstract factory #2
client2.Run();
Console.ReadKey();
/// <summary>
/// </summary>
/// <summary>
/// </summary>
/// <summary>
/// </summary>
/// <summary>
/// </summary>
}
/// <summary>
/// </summary>
/// <summary>
/// </summary>
/// <summary>
/// </summary>
Console.WriteLine(this.GetType().Name +
/// <summary>
/// </summary>
{
}
/// <summary>
/// </summary>
Console.WriteLine(this.GetType().Name +
/// <summary>
/// </summary>
class Client
// Constructor
_abstractProductB = factory.CreateProductB();
_abstractProductA = factory.CreateProductA();
_abstractProductB.Interact(_abstractProductA);
}
}
Builder
Separate the construction of a complex object from its representation so that the same construction
process can create different representations.
Factory Method
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory
Method lets a class defer instantiation to subclasses.
Sample code in C#
This structural code demonstrates the Factory method offering great flexibility in creating different objects.
The Abstract class may provide a default object, but each subclass can instantiate an extended version of
the object.
using System;
namespace DoFactory.GangOfFour.Factory.Structural
/// <summary>
/// </summary>
class MainApp
/// <summary>
/// </summary>
// An array of creators
Console.WriteLine("Created {0}",
product.GetType().Name);
Console.ReadKey();
/// <summary>
/// </summary>
/// <summary>
/// </summary>
}
/// <summary>
/// </summary>
/// <summary>
/// </summary>
/// <summary>
/// </summary>
/// <summary>
/// </summary>