Chapter Two (2)
Chapter Two (2)
Design Patterns
1
What are design patterns
In software engineering, a design pattern is a general repeatable solution to a
is a description or template for how to solve a problem that can be used in many
different situations.
Design patterns can speed up the development process by providing tested, proven
development paradigms.
Effective software design requires considering issues that may not become visible
problems and improves code readability for coders and architects familiar with the
patterns.
2
Cont…
Often, people only understand how to apply certain software
specific problem/task.
In addition, patterns allow developers to communicate using
3
Cont…
design patterns are programming language independent strategies for
architecture.
They provide transparency to the design of an application.
They are well-proved and testified solutions since they have been
objects.
These design patterns are used when a decision must be made at the time
object-creational patterns.
While class-creation patterns the process of creating instances of classes,
superclass but allows subclasses to alter the type of objects that will
be created.
Abstract Factory- Creates an instance of several families of classes.
interface or abstract class for creating an object but let the subclasses
decide which class to instantiate.
In other words, subclasses are responsible to create the instance of the
class.
The Factory Method Pattern is also known as Virtual Constructor.
objects to create.
It promotes the loose-coupling by eliminating the need to bind
create
When a class wants that its sub-classes specify the objects to be
created.
When the parent classes choose the creation of objects to its
sub-classes.
9
UML for Factory Method Pattern
We are going to create a Plan abstract class and concrete classes that extends the Plan
10
Abstract Factory Pattern
Abstract Factory Pattern says that just define an interface or abstract class for
the reason that Abstract Factory Pattern is one level higher than the Factory
Pattern.
An Abstract Factory Pattern is also known as Kit.
family of objects.
12
UML for Abstract Factory Pattern
We are going to create a Bank interface and a Loan abstract
13
Cont…
14
Singleton design pattern
Singleton Pattern says that just "define a class that has only one
16
How to create Singleton design pattern?
To create the singleton class, we need to have static member
File: A.java
class A{
private static A obj=new A();//Early, instance will be created at load time
private A(){}
public static A getA(){
return obj;
}
public void doSomething(){
//write your code
}
}
18
Prototype Design Pattern
Prototype Pattern says that cloning of an existing object instead
The clients can get new objects without knowing which type of
complicated.
When you want to keep the number of classes in an
application minimum.
When the client application needs to be unaware of object
20
UML for Prototype Pattern
21
Cont…
We are going to create an interface Prototype that contains a
22
Builder Design Pattern
Builder Pattern says that "construct a complex object from
representation of an object.
It provides better control over construction process.
23
UML for Builder Pattern Example
24
Cont…
Example of Builder Design Pattern
Samsung
Create the CDType class
25
Object Pool Pattern
Object Pool Pattern says that " to reuse the object that are
expensive to create".
Basically, an Object pool is a container which contains a
is high.
It manages the connections and provides a way to reuse and share them.
It can also provide the limit for the maximum number of objects that can be
created.
Usage:
there is a need of opening too many connections for the database then it
takes too longer to create a new one and the database server will be
overloaded.
When there are several clients who need the same resource at different
27
times.
UML for Object Pool Pattern
28
Structural design patterns
Structural design patterns are concerned with how classes and
It is used:
When an object needs to utilize an existing class with an
incompatible interface.
When you want to create a reusable class that cooperates with
32
Cont…
UML for Adapter Pattern
33
Bridge Pattern
A Bridge Pattern says that just "decouple the functional
34
Con…
Usage of Bridge Pattern
35
Composite Pattern
Compose objects into tree structures to represent whole-part
hierarchies.
Composite lets clients treat individual objects and compositions
of objects uniformly.
Recursive composition
36
Cont…
It is used:
objects.
When the responsibilities are needed to be added dynamically
37
Cont…
UML for Composite Pattern
38
Cont…
Elements used in Composite Pattern: the 4 elements of composite pattern.
1) Component
Declares interface for objects in composition.
Implements default behavior for the interface common to all classes as appropriate.
Declares an interface for accessing and managing its child components.
2) Leaf
Represents leaf objects in composition. A leaf has no children.
Defines behavior for primitive objects in the composition.
3) Composite
Defines behavior for components having children.
Stores child component.
Implements child related operations in the component interface.
4) Client
Manipulates objects in the composition through the component interface.
39
Cont…
Example of Composite Pattern
40
Decorator Pattern
A Decorator Pattern says that just "attach a flexible additional
42
Cont…
Example
43
Facade Pattern
A Facade Pattern says that just "just provide a unified and
components.
It promotes loose coupling between subsystems and its clients.
44
Cont…
It is used:
system.
When several dependencies exist between clients and the
45
Flyweight Pattern
A Flyweight Pattern says that just "to reuse already existing similar
access to it.
Use an extra level of indirection to support distributed, controlled, or
intelligent access.
Add a wrapper and delegation to protect the real component from
unnecessary complexity.
47
Proxy Pattern
Usage of Proxy Pattern :
It can be used in Virtual Proxy scenario---Consider a situation where there is multiple database call to
extract huge size image. Since this is an expensive operation so here we can use the proxy pattern which
would create multiple proxies and point to the huge size memory consuming object for further processing.
The real object gets created only when a client first requests/accesses the object and after that we can just
refer to the proxy to reuse the object. This avoids duplication of the object and hence saving memory.
It can be used in Protective Proxy scenario---It acts as an authorization layer to verify that whether the
actual user has access the appropriate content or not. For example, a proxy server which provides restriction
on internet access in office. Only the websites and contents which are valid will be allowed and the
remaining ones will be blocked.
It can be used in Remote Proxy scenario---A remote proxy can be thought about the stub in the RPC call.
The remote proxy provides a local representation of the object which is present in the different address
location. Another example can be providing interface for remote resources such as web service or REST
resources.
It can be used in Smart Proxy scenario---A smart proxy provides additional layer of security by
48 interposing specific actions when the object is accessed. For example, to check whether the real object is
locked or not before accessing it so that no other objects can change it.
Cont…
49
Behavioral Design Patterns
Behavioral design patterns are concerned with the interaction
should be in such a way that they can easily talk to each other
and still should be loosely coupled.
That means the implementation and the client should be loosely
50
Cont…
There are 12 types of behavioral design patterns:
receiver. If one object cannot handle the request then it passes the
52 same to the next receiver and so on.
Cont…
unknown.
When the group of objects that can handle the request must be
54
Command Pattern
A Command Pattern says that "encapsulate a request under an object
55 remain unchanged.
Cont…
Usage of command pattern:
57
Interpreter Pattern
An Interpreter Pattern says that "to define a representation of
59
Cont…
60
Iterator Pattern
Iterator Pattern is used "to access the elements of an aggregate
When there are multiple traversals of objects need to be supported in the collection.
62
Cont…
63
Mediator Pattern
A Mediator Pattern says that "to define an object that encapsulates how a set
of objects interact".
I will explain the Mediator pattern by considering a problem. When we begin
with development, we have a few classes and these classes interact with each
other producing results. Now, consider slowly, the logic becomes more complex
when functionality increases. Then what happens? We add more classes and
they still interact with each other but it gets really difficult to maintain this code
now. So, Mediator pattern takes care of this problem.
Mediator pattern is used to reduce communication complexity between
applications.
When the set of objects communicate in complex but in well-defined
65 ways.
Cont…
66
Memento Pattern
A Memento Pattern says that "to restore the state of an object to its
68
Observer Pattern
An Observer Pattern says that "just define a one-to-one
Subscribe.
Benefits:
69
Cont…
Usage:
When the change of a state in one object must be reflected in another object without
70
State Pattern
A State Pattern says that "the class behavior changes based on
its state".
In State Pattern, we create objects which represent various
Benefits:
It keeps the state-specific behavior.
Usage:
When the behavior of object depends on its state and it must be able to change its
behavior at runtime according to the new state.
It is used when the operations have large, multipart conditional statements that
depend on the state of an object
72
Strategy Pattern
A Strategy Pattern says that "defines a family of functionality, encapsulate
Benefits:
It provides a substitute to sub-classing.
It defines each behavior within its own class, eliminating the need for
conditional statements.
It makes it easier to extend and incorporate new behavior without changing
the application.
Usage:
When the multiple classes differ only in their behaviors. e.g. Servlet API.
It is used when you need different variations of an algorithm.
73
Cont…
UML for Strategy Pattern:
74
Template Pattern
A Template Pattern says that "just define the skeleton of a
Usage:
It is used when the common behavior among sub-classes
76