Professional Documents
Culture Documents
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.
1 private SingleTonTest()
2 {}
2 {
4 if(singleTonObject == null)
5 { lock (lockingObject)
6 {
7 if(singleTonObject == null)
8 {
10 }
11 }
12 }
13 return singleTonObject;
14 }
2{
4}
2 {}
7.Create an entry point to the above class by having a method name Main.
2{
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
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 }
7 {
8 }
11 {
12 }
13
15 {
17 }
18
20 {
22 {
23 switch (type)
24 {
28 }
29 }
30 }
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.
2 {
3 AbstractProductA CreateProductA();
4 AbstractProductB CreateProductB();
5 }
7 {
9 {
11 }
12
14 {
16 }
17 }
19 {
20 public AbstractProductA CreateProductA()
21 {
23 }
24
26 {
28 }
29 }
37 {
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.
2 {
3 Prototype Clone();
4 }
6 {
8 {
10 return (Prototype)MemberwiseClone();
11
13 //return (Prototype)this.Clone();
14 }
15 }
17 {
19 {
21 return (Prototype)MemberwiseClone();
22
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.
3 void BuildPart1();
4 void BuildPart2();
5 void BuildPart3();
6 Product GetProduct();
7 }
9 {
12 {
14 }
16 {
18 }
20 {
22 }
24 {
25 return _product;
26 }
27 }
29 {
33 }
35 {
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.