Professional Documents
Culture Documents
Singleton:
Ensure a class only has one instance.
Provide a global point of access to it.
1 private SingleTonTest()
2 {}
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.
2 {
4 if(singleTonObject == null)
5 { lock (lockingObject)
6 {
7 if(singleTonObject == null)
8 {
10 }
11 }
12 }
13 return singleTonObject;
14 }
4}
1 class Program
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:
Define an Interface for creating an object but let subclasses decide which class to
instantiate
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 {
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 :
1. Abstract Factory use the factory for creating objects of several classes.
2. Builder use the factory for creating a complex object by using simple objects and
a step by step approach.
3. Prototype use the factory for building a object by copying an existing object.
Prototype Pattern:
Prototype pattern specifies the kind of objects to create using a prototypical instance,
and create new objects by copying this prototype.
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.
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:
Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
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.
2 {
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 {
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 objects parts are
assembled.
Runtime control over the creation process is required.
In this article we will understand structural Design Patterns. That Includes:
Bridge Design Pattern is used when you want run-time binding of the
implementation,
Bridge Design Pattern is used when you have a proliferation of classes resulting
from a coupled interface and numerous implementations,
Bridge Design Pattern is used when you want to share an implementation among
multiple objects,
Bridge Design Pattern is used when you need to map orthogonal class
hierarchies.
Facade Design Pattern makes a software library easier to use, understand and
test
Facade Design Pattern make the library more readable
Facade Design Pattern reduce dependencies of outside code on the inner
workings of a library
Facade Design Pattern wrap a poorly designed collection of APIs with a single
well-designed API.
The classes, interfaces and objects in the above UML class diagram are as follows:
Complex System
A library of Testsystems.
TestsystemA, TestsystemB, TestsystemC
These are classes within complex system and offer detailed operations.
Faade
This is a wrapper class which contains a set of members which are required by client.
Client
This is a class which calls the high-level operations in the Faade.
1 class TestsystemA
2 {
3 public string OperationA1()
4 {
5 return "Testsystem A, Method A1\n";
6 }
7 public string OperationA2()
8 {
9 return "Testsystem A, Method A2\n";
10 }
11 }
12 class TestsystemB
13 {
14 public string OperationB1()
15 {
16 return "Testsystem B, Method B1\n";
17 }
18 public string OperationB2()
19 {
20 return "Testsystem B, Method B2\n";
21 }
22 }
23 class TestsystemC
24 {
25 public string OperationC1()
26 {
27 return "Testsystem C, Method C1\n";
28 }
29
30 public string OperationC2()
31 {
32 return "Testsystem C, Method C2\n";
33 }
34 }
35 public class Facade
36 {
37 TestsystemA a = new TestsystemA();
38 TestsystemB b = new TestsystemB();
39 TestsystemC c = new TestsystemC();
40 public void Operation1()
41 {
42 Console.WriteLine("Operation 1\n" +
43 a.OperationA1() +
44 a.OperationA2() +
45 b.OperationB1());
46 }
47 public void Operation2()
48 {
49 Console.WriteLine("Operation 2\n" +
50 b.OperationB2() +
51 c.OperationC1() +
52 c.OperationC2());
53 }
54 }
Flyweight shows how to make lots of little objects represent an entire subsystem.
Flyweight is often combined with Composite to implement shared leaf nodes.
Terminal symbols within Interpreters abstract syntax tree can be shared with
Flyweight.
Flyweight explains when and how State objects can be shared.
Abstraction
Encapsulation
Data Hiding
Separation of Concerns
Separation of Interface and Implementation
Single point of reference
Coupling and Cohesion etc..
Creational Patterns
o This type of pattern address problems of creating an object and separating it from
operations
Structural Patterns
o This type of pattern address problems of using object oriented constructs to organize
classes and objects
Behavioral Patterns
o This type of pattern address problems of assigning responsibilities to classes
Singleton
Factory
Abstract Factory
Prototype
Builder
Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize
relationships between entities.
Define an Interface for creating an object but let subclasses decide which class to instantiate
Lets a class defer instantiation to subclasses
Provides an interface for creating families of related or dependent objects without specifying their
concrete classes.
Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also
called as Factory of factories
You can see more details on Abstract Factory Design patterns here.
Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
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.
You can see more details on Builder Factory Design patterns here.
Bridge Pattern separates abstraction from its implementation, so that both can be modified
Independently
Bridge Pattern behaves like a bridge between abstraction class and Implementer class.
Composite pattern composes objects in term of a tree structure to represent part as well as whole
hierarchies.
Composite pattern creates a class contains group of its own objects. This class provides ways to
modify its group of same objects.
Composite pattern is used when we need to treat a group of objects and a single object in the
same way
Decorator pattern is used to add new functionality to an existing object without changing its
structure.
Decorators provide a flexible alternative to subclass for extending functionality.
This pattern creates a decorator class which wraps the original class and add new
behaviors/operations to an object at run-time.
Facade Design Pattern makes a software library easier to use, understand and test
Facade Design Pattern make the library more readable
Facade Design Pattern reduce dependencies of outside code on the inner workings of a library
Facade Design Pattern wrap a poorly designed collection of APIs with a single well-designed API.
Proxy Design pattern involves a class, called proxy class, which represents functionality of
another class.
Proxy is a wrapper or agent object that is being called by the client to access the real serving
object behind the scenes.