You are on page 1of 12

We will discuss creational patterns in detail.

In this article,
we will discuss:
o Singleton Design Pattern
o Factory Design Pattern
o Abstract Factory Design Pattern
o Prototype Design Pattern
o Builder Design Pattern

Singleton:
o Ensure a class only has one instance.
o Provide a global point of access to it.

Let’s see how to handle this.

1.Create a public Class (name SingleTonTest).

1 public class SingleTonTest{}

2.Define its constructor as private.

1 private SingleTonTest()

2 {}

3.Create a private static instance of the class (name singleTonObject).

1 private volatile static SingleTonTest singleTonObject;


4.Now write a static method (name InstanceCreation) which will be used to create an
instance of this class and return it to the calling method.

1 public static SingleTonTest InstanceCreation()

2 {

3 private static object lockingObject = new object();

4 if(singleTonObject == null)

5 { lock (lockingObject)

6 {

7 if(singleTonObject == null)

8 {

9 singleTonObject = new SingleTonTest();

10 }

11 }

12 }

13 return singleTonObject;

14 }

5.Create a public method in this class.

1 public void DisplayMessage()

2{

3 Console.WriteLine("My First SingleTon Program");

4}

6.Now we will create an another Class (name Program).


1 class Program

2 {}

7.Create an entry point to the above class by having a method name Main.

1 static void Main(string[] args)

2{

3 SingleTonTest singleton = SingleTonTest.InstanceCreation();

4 singleton.DisplayMessage();

5 Console.ReadLine();

6}

Factory Method:
o Define an Interface for creating an object but let subclasses decide which class to
instantiate
o Lets a class defer instantiation to subclasses

Let’s see how to achieve this.

1.Create an interface customer for creating the objects.

2. Now create a class customerClass which implements from customer interface.

3. Create an abstract class classA and declares the factory method, which returns an
object of type Customer.

4.Now create a class classB which implements the classA and overrides the factory
method to return an instance of a customerclass.
1 interface customer

2 {

4 }

6 class customerclassA : customer

7 {

8 }

10 class customerclassB: customer

11 {

12 }

13

14 abstract class classA

15 {

16 public abstract customer FactoryMethod(string type);

17 }

18

19 class classB: classA

20 {

21 public override customer FactoryMethod(string type)

22 {

23 switch (type)

24 {

25 case "A": return new customerclassA();

26 case "B": return new customerclassB();


27 default: throw new ArgumentException("Invalid type", "type");

28 }

29 }

30 }

Abstract Factory Pattern:


o Provides an interface for creating families of related or dependent objects
without specifying their concrete classes.
o Abstract Factory patterns acts a super-factory which creates other factories. This
pattern is also called as Factory of factories

1.AbstractFactory
This is an interface which is used to create abstract product
2.ConcreteFactory
This is a class which implements the AbstractFactory interface to create concrete
products.
3.AbstractProduct
This is an interface which declares a type of product.
4.ConcreteProduct
This is a class which implements the AbstractProduct interface to create product.
5.Client
This is a class which use AbstractFactory and AbstractProduct interfaces to create a
family of related objects.

Let’s look at the Implementation:

1 public interface AbstractFactory

2 {

3 AbstractProductA CreateProductA();

4 AbstractProductB CreateProductB();

5 }

6 public class ConcreteFactoryA : AbstractFactory

7 {

8 public AbstractProductA CreateProductA()

9 {

10 return new ProductA1();

11 }

12

13 public AbstractProductB CreateProductB()

14 {

15 return new ProductB1();

16 }

17 }

18 public class ConcreteFactoryB : AbstractFactory

19 {
20 public AbstractProductA CreateProductA()

21 {

22 return new ProductA2();

23 }

24

25 public AbstractProductB CreateProductB()

26 {

27 return new ProductB2();

28 }

29 }

30 public interface AbstractProductA { }

31 public class ProductA1 : AbstractProductA { }

32 public class ProductA2 : AbstractProductA { }

33 public interface AbstractProductB { }

34 public class ProductB1 : AbstractProductB { }

35 public class ProductB2 : AbstractProductB { }

36 public class Client

37 {

38 private AbstractProductA _productA;

39 private AbstractProductB _productB;

40 public Client(AbstractFactory factory)

41 {

42 _productA = factory.CreateProductA();

43 _productB = factory.CreateProductB();

44 }

45 }
1. Abstract Factory use Factory design pattern for creating objects. But it can also
use Builder design pattern and prototype design pattern for creating objects. It
completely depends upon your implementation for creating objects.
2. Abstract Factory can be used as an alternative to Facade to hide platform-specific
classes.
3. When Abstract Factory, Builder, and Prototype define a factory for creating the
objects, we should consider the following points :
I. Abstract Factory use the factory for creating objects of several classes.
II. Builder use the factory for creating a complex object by using simple
objects and a step by step approach.
III. Prototype use the factory for building a object by copying an existing
object.

Prototype Pattern:
o Prototype pattern specifies the kind of objects to create using a prototypical
instance, and create new objects by copying this prototype.
o It is used to create a duplicate object or clone of the current object to enhance
performance.

1.Prototype
This is an interface which is used for the types of object that can be cloned itself.
2.ConcretePrototype
This is a class which implements the Prototype interface for cloning itself.

Let’s see how to achieve this.


1 public interface Prototype

2 {

3 Prototype Clone();

4 }

5 public class ConcretePrototypeA : Prototype

6 {

7 public Prototype Clone()

8 {

9 // Shallow Copy: only top-level objects are duplicated

10 return (Prototype)MemberwiseClone();

11

12 // Deep Copy: all objects are duplicated

13 //return (Prototype)this.Clone();

14 }

15 }

16 public class ConcretePrototypeB : Prototype

17 {

18 public Prototype Clone()

19 {

20 // Shallow Copy: only top-level objects are duplicated

21 return (Prototype)MemberwiseClone();

22

23 // Deep Copy: all objects are duplicated

24 //return (Prototype)this.Clone();

25 }

26 }

Builder Pattern:
o Separate the construction of a complex object from its representation so that the
same construction process can create different representations.
o In other words,you will have to design the system in such a way that the client
application will simply specify the parameters that should be used to create the
complex object and the builder will take care of building the complex object.

Description:

1.Builder
This is an interface which is used to define all the steps to create a product
2.ConcreteBuilder
This is a class which implements the Builder interface to create complex product.
3.Product
This is a class which defines the parts of the complex object which are to be generated by
the builder pattern.
4.Director
This is a class which is used to construct an object using the Builder interface.

Let’s see how to achieve this.

1 public interface IBuilder


2 {

3 void BuildPart1();

4 void BuildPart2();

5 void BuildPart3();

6 Product GetProduct();

7 }

8 public class ConcreteBuilder : IBuilder

9 {

10 private Product _product = new Product();

11 public void BuildPart1()

12 {

13 _product.Part1 = "Part 1";

14 }

15 public void BuildPart2()

16 {

17 _product.Part2 = "Part 2";

18 }

19 public void BuildPart3()

20 {

21 _product.Part3 = "Part 3";

22 }

23 public Product GetProduct()

24 {

25 return _product;

26 }

27 }

28 public class Product

29 {

30 public string Part1 { get; set; }


31 public string Part2 { get; set; }

32 public string Part3 { get; set; }

33 }

34 public class Director

35 {

36 public void Construct(IBuilder IBuilder)

37 {

38 IBuilder.BuildPart1();

39 IBuilder.BuildPart2();

40 IBuilder.BuildPart3();

41 }

42 }

The creation of objects should be independent from the way the object’s parts are
assembled.
Runtime control over the creation process is required.

You might also like