SlideShare a Scribd company logo
1 of 25
Topics : Design patterns in java
created by……
Md .shohel Rana
Roll:140135
2nd year 2nd semester
Dept. of Cse
PUST
introduction
 What is design patterns ?
 Why need design patterns ?
 What types of design patterns ?
 Describing design patterns.
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
Why need design pattern
To design a new software system quickly and efficiently.
To understand a existing software system.
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.
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.
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.
 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
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.
Example:1
BangladeshPresident.java
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
}
}
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;
}
Example 2:
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.
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.
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.
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.
UML class diagram for decorator design
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.
Example
gvhghBuilding
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.
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.
Any questions……?
thanks

More Related Content

What's hot

Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan GoleChetan Gole
 
Java Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaEdureka!
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design PatternKanushka Gayan
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1Shahzad
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsMichael Heron
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)paramisoft
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design PatternJaswant Singh
 
OMD chapter 2 Class modelling
 OMD  chapter 2 Class modelling OMD  chapter 2 Class modelling
OMD chapter 2 Class modellingjayashri kolekar
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade PatternMudasir Qazi
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in androidJay Kumarr
 
Gof design pattern
Gof design patternGof design pattern
Gof design patternnaveen kumar
 

What's hot (20)

Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
 
Java Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | Edureka
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design Patterns
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
OMD chapter 2 Class modelling
 OMD  chapter 2 Class modelling OMD  chapter 2 Class modelling
OMD chapter 2 Class modelling
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
UML
UMLUML
UML
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 

Similar to What is design pattern

P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
Why Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringProtelo, Inc.
 
Sofwear deasign and need of design pattern
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design patternchetankane
 
Patterns for the People
Patterns for the PeoplePatterns for the People
Patterns for the PeopleKevlin Henney
 
Gang of Four in Java
Gang of Four in Java Gang of Four in Java
Gang of Four in Java Mina Tafreshi
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Patterneprafulla
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsLalit Kale
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro anshu_atri
 
Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldSaurabh Moody
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patternssukumarraju6
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityShubham Narkhede
 
Solid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignSolid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignIrwansyah Irwansyah
 

Similar to What is design pattern (20)

P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Why Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software Engineering
 
Sofwear deasign and need of design pattern
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design pattern
 
Patterns for the People
Patterns for the PeoplePatterns for the People
Patterns for the People
 
Gang of Four in Java
Gang of Four in Java Gang of Four in Java
Gang of Four in Java
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
 
Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
 
Sda 8
Sda   8Sda   8
Sda 8
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Solid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignSolid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven Design
 

Recently uploaded

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 

What is 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.
  • 18. UML class diagram for decorator design
  • 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.