0% found this document useful (0 votes)
49 views

'Structural' 'Real-World': Creational Patterns

Design patterns are recurring solutions to common programming problems. The Gang of Four (GoF) patterns are considered foundational, including creational patterns like abstract factory and factory method that create objects, and structural patterns like adapter and bridge that compose objects. The document provides code examples in C# of abstract factory, builder, and factory method patterns.

Uploaded by

ankita_agg1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

'Structural' 'Real-World': Creational Patterns

Design patterns are recurring solutions to common programming problems. The Gang of Four (GoF) patterns are considered foundational, including creational patterns like abstract factory and factory method that create objects, and structural patterns like adapter and bridge that compose objects. The document provides code examples in C# of abstract factory, builder, and factory method patterns.

Uploaded by

ankita_agg1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Design patterns are recurring solutions to software design problems you find again and again in real-

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

Abstract Factory Creates an instance of several families of classes

Builder Separates object construction from its representation

Factory Method Creates an instance of several derived classes

Prototype A fully initialized instance to be copied or cloned

Singleton A class of which only a single instance can exist

Structural Patterns

Adapter Match interfaces of different classes

Bridge Separates an object’s interface from its implementation

Composite A tree structure of simple and composite objects

Decorator Add responsibilities to objects dynamically

Facade A single class that represents an entire subsystem

Flyweight A fine-grained instance used for efficient sharing

Proxy An object representing another object


Behavioral Patterns

Chain of Resp. A way of passing a request between a chain of objects

Command Encapsulate a command request as an object

Interpreter A way to include language elements in a program

Iterator Sequentially access the elements of a collection

Mediator Defines simplified communication between classes

Memento Capture and restore an object's internal state

Observer A way of notifying change to a number of classes

State Alter an object's behavior when its state changes

Strategy Encapsulates an algorithm inside a class

Template Method Defer the exact steps of an algorithm to a subclass

Visitor Defines a new operation to a class without change

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>

/// MainApp startup class for Structural

/// Abstract Factory Design Pattern.

/// </summary>

class MainApp

/// <summary>
/// Entry point into console application.

/// </summary>

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();

/// <summary>

/// The 'AbstractFactory' abstract class

/// </summary>

abstract class AbstractFactory

public abstract AbstractProductA CreateProductA();

public abstract AbstractProductB CreateProductB();

/// <summary>

/// The 'ConcreteFactory1' class

/// </summary>

class ConcreteFactory1 : AbstractFactory


{

public override AbstractProductA CreateProductA()

return new ProductA1();

public override AbstractProductB CreateProductB()

return new ProductB1();

/// <summary>

/// The 'ConcreteFactory2' class

/// </summary>

class ConcreteFactory2 : AbstractFactory

public override AbstractProductA CreateProductA()

return new ProductA2();

public override AbstractProductB CreateProductB()

return new ProductB2();

/// <summary>

/// The 'AbstractProductA' abstract class

/// </summary>

abstract class AbstractProductA

}
/// <summary>

/// The 'AbstractProductB' abstract class

/// </summary>

abstract class AbstractProductB

public abstract void Interact(AbstractProductA a);

/// <summary>

/// The 'ProductA1' class

/// </summary>

class ProductA1 : AbstractProductA

/// <summary>

/// The 'ProductB1' class

/// </summary>

class ProductB1 : AbstractProductB

public override void Interact(AbstractProductA a)

Console.WriteLine(this.GetType().Name +

" interacts with " + a.GetType().Name);

/// <summary>

/// The 'ProductA2' class

/// </summary>

class ProductA2 : AbstractProductA

{
}

/// <summary>

/// The 'ProductB2' class

/// </summary>

class ProductB2 : AbstractProductB

public override void Interact(AbstractProductA a)

Console.WriteLine(this.GetType().Name +

" interacts with " + a.GetType().Name);

/// <summary>

/// The 'Client' class. Interaction environment for the products.

/// </summary>

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);

}
}

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.

// Factory Method pattern -- Structural example

using System;

namespace DoFactory.GangOfFour.Factory.Structural

/// <summary>

/// MainApp startup class for Structural

/// Factory Method Design Pattern.

/// </summary>

class MainApp

/// <summary>

/// Entry point into console application.

/// </summary>

static void Main()


{

// An array of creators

Creator[] creators = new Creator[2];

creators[0] = new ConcreteCreatorA();

creators[1] = new ConcreteCreatorB();

// Iterate over creators and create products

foreach (Creator creator in creators)

Product product = creator.FactoryMethod();

Console.WriteLine("Created {0}",

product.GetType().Name);

// Wait for user

Console.ReadKey();

/// <summary>

/// The 'Product' abstract class

/// </summary>

abstract class Product

/// <summary>

/// A 'ConcreteProduct' class

/// </summary>

class ConcreteProductA : Product

}
/// <summary>

/// A 'ConcreteProduct' class

/// </summary>

class ConcreteProductB : Product

/// <summary>

/// The 'Creator' abstract class

/// </summary>

abstract class Creator

public abstract Product FactoryMethod();

/// <summary>

/// A 'ConcreteCreator' class

/// </summary>

class ConcreteCreatorA : Creator

public override Product FactoryMethod()

return new ConcreteProductA();

/// <summary>

/// A 'ConcreteCreator' class

/// </summary>

class ConcreteCreatorB : Creator

public override Product FactoryMethod()


{

return new ConcreteProductB();

You might also like