what is design pattern?
what is design pattern in java?
what are the classification of design pattern?
why design pattern is important?
how we can implement any design pattern?
example of design pattern.
1. Topics : Design patterns in java
created by……
Md .shohel Rana
Roll:140135
2nd year 2nd semester
Dept. of Cse
PUST
2. introduction
What is design patterns ?
Why need design patterns ?
What types of design patterns ?
Describing design patterns.
3. Design pattern is a general reusable solution to a commonly
occurring Problem in software design.
A design pattern is not a finished design that can be
transformed directly into code . It is a description or template
for how to solve a problem that can be used in many different
situations.
A designer who is familiar with such patterns can apply them
immediately to design problems without having to rediscover
them.
What is Design pattern
4. Why need design pattern
To design a new software system quickly and efficiently.
To understand a existing software system.
5. Types of Design pattern
Creational design pattern.
--concerned with everything about the creation of objects.
Structural design patterns.
--concerned with how classes and objects are composed to
form larger structures.
Behavioural design patterns.
--communication between objects.
6. 1) Singleton design pattern(Creational pattern)
Ensure that only one instance of a class is created.
Provide a global point of access to the object.
In other words, a class must ensure that only single
instance should be created and single object can be used
by all other classes.
7. How to create Singleton design pattern?
To create the singleton class, we need to have static member of class,
private constructor and static factory method.
Static member: It gets memory only once because of static,
itcontains the instance of the Singleton class.
Private constructor: It will prevent to instantiate the Singleton class
from outside the class.
Static factory method: This provides the global point of access to the
Singleton object and returns the instance to the caller.
8. We need.
1) Private static instance of class
2) Private constractor
3) Public static method with return type of class to access that instance.
UML -class diagram for singleton
9. There are two forms of singleton design pattern
1) Early mode: creation of instance at load time.
2) Lazy mode: creation of instance when required.
11. Understanding early mode of Singleton Pattern
class BangladeshPresident{
private static PresidentObj=new President();//Early, instance will be created at load
time
private BangladeshPresident(){}
public static A getPresident (){
return PresidentObj;
}
public void doSomething(){
//write your code
}
}
12. Understanding lazy modeof Singleton Pattern
class BangladeshPresident{
private static A PresidentObj;
private BangladeshPresident(){}
public static A getPresident (){
if (PresidentObj == null){
PresidentObj = new BangladeshPresident();//instance will be
created at request time
}
return PresidentObj;
}
14. Advantage of Singleton design pattern
Saves memory because object is not created at each
request.only single instance is reused again and again.
In cases when object creation is very costly (time taking),
we don’t have to create new object each time we need it.
We just access already created object.
15. Applications
Singleton pattern is mostly used in multi-threaded and database
applications.
It is used in logging ,configuration settings etc.
For database connection , because one connection is enough for
most applications and too much connections can make application
slow.
16. 2) Decorator design pattern (Structural Patterns)
Decorator design pattern is used to modify the functionality or
characteristics of an object at runtime or dynamically. At the same
time other instances of the same class will not be affected by this,
so individual object gets the new modified behavior.Unlike
inheritance, you can choose any single object of a class and modify
its behaviour leaving the other instances unmodified.
In implementing the decorator pattern you construct a wrapper
around an object by extending its behavior. The wrapper will do its
job before or after and delegate the call to the wrapped instance.
17. When to Use decorate design pattern
We use inheritance or composition to extend the behavior of an object but this is
done at compile time and its applicable to all the instances of the class. We
can’t add any new functionality of remove any existing behavior at runtime – this
is when Decorator pattern comes into picture.
to add responsibilities to individual objects dynamically without affecting other
objects.
When extension by subclassing is impractical. Sometimes, a large number of
independent extensions are possible and would produce an explosion of
subclasses to support every combination. Or class definition may be hidden or
otherwise unavailable for subclassing.
19. Elements
Component
defines interface for objects that can have additional responsibilities
added to them.
ConcreteComponent
defines an object on which additional responsibilities can be added
Decorator
maintains a reference to component object and defines an interface that
conforms to component's interface. In simple words, it has a component
object to be decorated.
ConcreteDecorator
add responsibilities to the component object.
22. Advantages of Decorator Design Pattern
It is flexible than inheritance because inheritance adds
responsibility at compile time but decorator pattern adds
at run time.
We can have any number of decorators and also in any
order.
It extends functionality of object without affecting any
other object.
23. Disadvantage of Decorator Design Pattern
The main disadvantage of decorator design pattern is
code maintainability because this pattern creates lots of
similar decorators which are sometimes hard to maintain
and distinguish.