You are on page 1of 32

Creational Design Patterns

We will discuss creational patterns in detail. In this article, we


will discuss:
Singleton Design Pattern
Factory Design Pattern
Abstract Factory Design Pattern
Prototype Design Pattern
Builder Design Pattern

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

Lets 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:
Define an Interface for creating an object but let subclasses decide which class to
instantiate
Lets a class defer instantiation to subclasses

Lets 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:


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

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.

Lets 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 :
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.

Lets 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:
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.

Lets 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 objects parts are
assembled.
Runtime control over the creation process is required.
In this article we will understand structural Design Patterns. That Includes:

Adapter Design Pattern


Bridge Design Pattern
Composite Design Pattern
Decorator Design Pattern
Faade Design Pattern
Flyweight Design Pattern
Proxy Design Pattern

Adapter Design Pattern:


The adapter pattern is adapting between classes and objects
This pattern involves a single class called adapter which is responsible for
communication between two independent or incompatible interfaces
This works like a bridge between two incompatible interfaces

UML Class Diagram and Implementation:


ITarget
This is an interface which is used by the client to achieve its functionality/request.
Adapter
This is a class which implements the ITarget interface and inherits the Adaptee class. It is used for
communication between Client and Adaptee.
Adaptee
Adaptee class have the functionality that is required by the client. However, its interface is not
compatible with the client.
Client
This is a class which interact with a type that implements the ITarget interface. The communication class
adaptee, is not compatible with the client

Lets see how to implement it.

1 public class Client


2 {
3 private ITarget target;
4
5 public Client(ITarget target)
6 {
7 this.target = target;
8 }
9 public void Request()
10 {
11 target.TestMethodA();
12 }
13 }
14 public interface ITarget
15 {
16 void TestMethodA();
17 }
18
19 public class Adapter : Adaptee, ITarget
20 {
21 public void TestMethodA()
22 {
23 TestMethodB();
24 }
25 }
26 public class Adaptee
27 {
28 public void TestMethodB()
29 {
30 Console.WriteLine("TestMethodB() is called");
31 }
32 }

When to use Adapter Design Pattern:

Adapter makes things work after theyre designed


Adapter provides a different interface to its subject
Adapter is meant to change the interface of an existing object

Bridge Design Pattern:


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.

UML Class Diagram and Implementation:


The classes, interfaces and objects in the above UML class diagram are as follows:
Abstraction
This is an abstract class and containing members that define an abstract business object and its
functionality. It contains a reference to an object of type Bridge. It can also acts as the base class for
other abstractions.
Redefined Abstraction
This is a class which inherits from the Abstraction class. It extends the interface defined by Abstraction
class.
Bridge
This is an interface which acts as a bridge between the abstraction class and implementer classes and
also makes the functionality of implementer class independent from the abstraction class.
ImplementationA & ImplementationB
These are classes which implement the Bridge interface and also provide the implementation details for
the associated Abstraction class.

Lets see how to implement Bridge Design Pattern.

1 public abstract class Abstraction


2 {
3 public Bridge Implementer { get; set; }
4
5 public virtual void Operation()
6 {
7 Console.WriteLine("ImplementationBase:Operation()");
8 Implementer.OperationImplementation();
9 }
10 }
11 public class RefinedAbstraction : Abstraction
12 {
13 public override void Operation()
14 {
15 Console.WriteLine("RefinedAbstraction:Operation()");
16 Implementer.OperationImplementation();
17 }
18 }
19 public interface Bridge
20 {
21 void OperationImplementation();
22 }
23
24 public class ImplementationA : Bridge
25 {
26 public void OperationImplementation()
27 {
28 Console.WriteLine("ImplementationA:OperationImplementation()");
29 }
30 }
31 public class ImplementationB : Bridge
32 {
33 public void OperationImplementation()
34 {
35 Console.WriteLine("ImplementationB:OperationImplementation()");
36 }
37 }

When to use Bridge Design Pattern:

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.

Composite Design Pattern:


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

UML Class Diagram and Implementation:


The classes, interfaces and objects in the above UML class diagram are as follows:
Component
This is an abstract class containing members that will be implemented by all object in the hierarchy. It
acts as the base class for all the objects within the hierarchy
Composite
This is a class which includes Add,Remove,Find and Get methods to do operations on Employee
components.
Leaf
This is a class which is used to define leaf components within the tree structure means these cannot have
TotalEmployee.

Lets see how to implement this.

1 public interface Component


2 {
3 void Operation();
4 }
5 public class Composite : Component, IEnumerable
6 {
7 private List _TotalEmployee = new List();
8 public void AddEmployee(Component Employee)
9 {
10 _TotalEmployee.Add(Employee);
11 }
12 public void RemoveEmployee(Component Employee)
13 {
14 _TotalEmployee.Remove(Employee);
15 }
16 public Component GetEmployee(int index)
17 {
18 return _TotalEmployee[index];
19 }
20 public void Operation()
21 {
22 string message = string.Format("Composite with {0} Employee(ren)", _TotalEmployee.Count);
23 Console.WriteLine(message);
24 }
25 public IEnumerator GetEnumerator()
26 {
27 foreach (Component Employee in _TotalEmployee)
28 yield return Employee;
29 }
30 IEnumerator IEnumerable.GetEnumerator()
31 {
32 return GetEnumerator();
33 }
34 }
35 public class Leaf : Component
36 {
37 public void Operation()
38 {
39 Console.WriteLine("Leaf");
40 }
41 }

When to use Composite Design Pattern

Composite Design Pattern rely on recursive composition to organize an open-


ended number of objects.
Composite Design Pattern can be traversed with Iterator.
Composite Design Pattern can let you compose a Mediator out of smaller pieces
through recursive composition.
Flyweight Design Pattern is often combined with Composite Design Pattern to
implement shared leaf
nodes.

Decorator Design Pattern:

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.

UML Class Diagram and Implementation:


The classes, interfaces and objects in the above UML class diagram are as follows:
Component
This is an interface containing members that will be implemented by ConcreteClass and Decorator.
ConcreteComponent
This is a class which implements the Component interface.
Decorator
This is an abstract class which implements the Component interface and contains the reference to a
Component instance. This class also acts as base class for all decorators for components.
ConcreteDecorator
This is a class which inherits from Decorator class and provides a decorator for components.

Lets see how to implement it.

1 public interface Component


2 {
3 void Operation();
4 }
5 public class ConcreteComponent : Component
6 {
7 public void Operation()
8 {
9 Console.WriteLine("Component Operation");
10 }
11 }
12 public abstract class Decorator : Component
13 {
14 private Component _component;
15
16 public Decorator(Component component)
17 {
18 _component = component;
19 }
20 public virtual void Operation()
21 {
22 _component.Operation();
23 }
24 }
25 public class ConcreteDecorator : Decorator
26 {
27 public ConcreteDecorator(Component component) : base(component) { }
28 public override void Operation()
29 {
30 base.Operation();
31 Console.WriteLine("Override Decorator Operation");
32 }
33 }

When to use Decorator Design Pattern:

Decorator Design Pattern provides an enhanced interface.


Decorator Design Pattern enhances an objects responsibilities so it is more
transparent to client
Decorator Design Pattern rely on recursive composition to organize an open-
ended number of objects.
Decorator Design Pattern can be viewed as a degenerate Composite with only
one component
Decorator Design Pattern provide a level of indirection to another object, and the
implementations keep a reference to the object to which they forward request

Facade Design Pattern:

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.

UML Class Diagram and Implementation:

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.

Lets see how to implement it.

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 }

When to use Facade Design Pattern:

Facade defines a new interface


Facade shows how to make a single object represent an entire subsystem.
Facade abstracts functionality of existing classes
Abstract Factory can be used as an alternative to Facade to hide platform-
specific classes
Facade objects are often Singletons because only one Facade object is required
Adapter and Facade are both wrappers; but they are different kinds of wrappers

Flyweight Design Pattern:

Flyweight design pattern is an object that minimizes memory use by sharing as


much data as possible with other similar objects
Flyweight pattern is used to reduce the number of objects created, to decrease
memory and resource usage. As a result it increase performance
Flyweight design pattern provides a way to use objects in large numbers when a
simple repeated representation would use an unacceptable amount of memory.
The flyweight pattern uses the concepts of intrinsic and extrinsic data.Intrinsic
datais held in the properties of the shared flyweight objects. This information is
stateless and generally remains unchanged, if any change occurs it would be
reflected among all of the objects that reference the flyweight.Extrinsic data is
computed on the fly means at runtime and it is held outside of a flyweight object.
Hence it can be stateful.

UML Class Diagram for Flyweight Design Pattern:


The classes, interfaces and objects in the above UML class diagram are as follows:
Flyweight
This is an interface which defines the members of the flyweight objects.
ConcreteFlyweight
This is a class which Inherits from the Flyweight class.
UnsharedFlyweight
This is a class which Inherits from the Flyweight class and enables sharing of information, it is possible to
create instances of concrete flyweight classes that are not shared.
FlyweightFactory
This is a class which holds the references of already created flyweight objects. When the GetFlyweight
method is called from client code, these references are checked to determine if an appropriate flyweight
object is already present or not. If present, it is returned. Otherwise a new object is generated, added to
the collection and returned.

Lets see how to Implement it.

1 public class FlyweightFactory


2 {
3 private Hashtable _flyweights = new Hashtable();
4
5 public Flyweight GetFlyweight(string key)
6 {
7 if (_flyweights.Contains(key))
8 {
9 return _flyweights[key] as Flyweight;
10 }
11 else
12 {
13 ConcreteFlyweight newFlyweight = new ConcreteFlyweight();
14
15 // Set properties of new flyweight here.
16
17 _flyweights.Add(key, newFlyweight);
18 return newFlyweight;
19 }
20 }
21 }
22 public interface Flyweight
23 {
24 void StatefulOperation(object o);
25 }
26
27 public class ConcreteFlyweight : Flyweight
28 {
29 public void StatefulOperation(object o)
30 {
31 Console.WriteLine(o);
32 }
33 }
34 public class UnsharedFlyweight : Flyweight
35 {
36 private object _state;
37
38 public void StatefulOperation(object o)
39 {
40 _state = o;
41 Console.WriteLine(o);
42 }
43 }

When to use Flyweight Design Pattern:

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.

Proxy Design Pattern:


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.

UML Class Diagram for Proxy Design Pattern:


The classes, interfaces and objects in the above UML class diagram are as follows:
Subject
This is an interface having members that will be implemented by RealSubject and Proxy class.
RealSubject
This is a class which we want to use more efficiently by using proxy class.
Proxy
This is a class which holds the instance of RealSubject class and can access RealSubject class members
as required.

1 public interface Subject


2 {
3 void PerformOperation();
4 }
5 public class RealSubject : Subject
6 {
7 public void PerformOperation()
8 {
9 Console.WriteLine("RealSubject action performed.");
10 }
11 }
12 public class Proxy : Subject
13 {
14 private RealSubject _realSubject;
15
16 public void PerformOperation()
17 {
18 if (_realSubject == null)
19 _realSubject = new RealSubject();
20
21 _realSubject.PerformOperation();
22 }
23 }

When to use Proxy Design Pattern:

Proxy Design Patterns provides the same interface to its subject


Proxy Design Pattern describes how to provide a level of indirection to another
object, and the implementations keep a reference to the object to which they
forward requests.

1.What is Design Pattern?


A design pattern is a general reusable solution to a commonly occurring problem within a given context in
software design. A design pattern is not a finished design that can be transformed directly into source or
machine code. It is a description or template for how to solve a problem that can be used in many
different situations. Patterns are formalized best practices that the programmer can use to solve common
problems when designing an application or system

2.What all are the advantages of Design Patterns?


Patterns solve software structural and non functional problems.

Structural problems like:

Abstraction
Encapsulation
Data Hiding
Separation of Concerns
Separation of Interface and Implementation
Single point of reference
Coupling and Cohesion etc..

Non Functional problems like:


Efficiency
Reliability
Interoperability
Testability
Reusability etc..

3.What all are the types of Design Patterns?


There are 3 types of Design Pattern.

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

4.What is Creational Design Pattern?


In software engineering, creational design patterns are design patterns that deal with object creation
mechanisms, trying to create objects in a manner suitable to the situation.

In C#, we have 5 types of Design Patterns in Creational Catagory.

Singleton
Factory
Abstract Factory
Prototype
Builder

5.What is Structural Design Pattern?


Structural patterns are concerned with how classes and objects are composed to form
larger structures; the class form of the Adapterdesign pattern is an example.

Structural class patterns use inheritance to compose interface or implementations.

Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize
relationships between entities.

In C#, We have 7 types of design patterns in Structural Catagory.


Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

6.What is Singleton Design Pattern?


Singleton ensures a class only has one instance.
Singleton Provides a global point of access to it.

You can see more details on Singleton Design patterns here.

7.What is Factory Design Pattern?


Factory Design patterns:

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

You can see more details on Factory Design patterns here.

8.What is Abstract Factory Design Pattern?


Abstract Factory Design patterns:

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.

9.What is Prototype Design Pattern?


Prototype Design patterns:
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.

You can see more details on Prototype Design patterns here.

10.What is Builder Design Pattern?


Builder Design patterns:

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.

11.What is Adapter Design Pattern?


Adapter Design patterns:

The adapter pattern is adapting between classes and objects


This pattern involves a single class called adapter which is responsible for communication
between two independent or incompatible interfaces
This works like a bridge between two incompatible interfaces

You can see more details on Abstract Design patterns here.

12.What is Bridge Design Pattern?


Bridge Design patterns:

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.

You can see more details on Bridge Design patterns here.


13.What is Composite Design Pattern?
Composite Design patterns:

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

You can see more details on Composite Design patterns here.

14.What is Decorator Design Pattern?


Decorator Design patterns:

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.

You can see more details on Decorator Design patterns here.

15.What is Facade Design Pattern?


Facade Design patterns:

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.

You can see more details on Facade Design patterns here.

16.What is Flyweight Design Pattern?


Flyweight Design patterns:
Flyweight design pattern is an object that minimizes memory use by sharing as much data as
possible with other similar objects
Flyweight pattern is used to reduce the number of objects created, to decrease memory and
resource usage. As a result it increase performance
Flyweight design pattern provides a way to use objects in large numbers when a simple repeated
representation would use an unacceptable amount of memory.
The flyweight pattern uses the concepts of intrinsic and extrinsic data.Intrinsic datais held in the
properties of the shared flyweight objects. This information is stateless and generally remains
unchanged, if any change occurs it would be reflected among all of the objects that reference the
flyweight.Extrinsic data is computed on the fly means at runtime and it is held outside of a
flyweight object. Hence it can be stateful.

You can see more details on Flyweight Design patterns here.

17.What is Proxy Design Pattern?


Proxy Design patterns:

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.

You might also like