SlideShare a Scribd company logo
Decorator Design Pattern
Ahmad Alkhous
Damascus University
alkhousahmad@gmail.com
decorator pattern is a design pattern that allows behavior to be added
to an individual object, dynamically, without affecting the behavior of
other objects from the same class
What is Decorator design pattern?
What problems can the Decorator design
pattern solve?
• Responsibilities should be added to (and removed from) an object
dynamically at run-time.
• A flexible alternative to subclassing for extending functionality should
be provided.
What is the problem with subclassing ?
• With number of various functionalities that can be mixed together
Here comes the BIG problem
Who is hungry?
Beef Slice
Bread
Lettuce Cheddar Egg Mayo Ketchup
How to calculate the price of the order
Beef Slice
Bread
Lettuce Cheddar Egg Mayo Ketchup
5
10 3 2 5 1 1
How to calculate the price of the order
How to calculate the price of the order
interface Icalculate {
public double calculate();
}
How to calculate the price of the order
interface Icalculate {
public double calculate();
}
class BeefMeal:Icalculate{
..
..
public double calculate() {
return 10;
}
..
}
How to calculate the price of the order
interface Icalculate {
public double calculate();
}
class BeefMeal:Icalculate{
..
..
public double calculate() {
return 10;
}
..
}
class BeefMealWithLettuce : Icalculate
{
..
..
public double calulate()
{
return 10 +3 ;
}
}
Decorator UML Diagram
Our Burger Meal revisited
Demo
So Where is the Trick ?
So Where is the Trick ?
So Where is the Trick ?
• This allows multiple decorators to be
stacked on top of each other, each time
adding a new functionality to the overridden
method(s)
Another Famous Example – Java IO
//First open an inputstream of it:
FileInputStream fis = new FileInputStream("/objects.gz");
//We want speed, so let's buffer it in memory:
BufferedInputStream bis = new BufferedInputStream(fis);
//The file is gzipped, so we need to ungzip it:
GzipInputStream gis = new GzipInputStream(bis);
//We need to unserialize those Java objects:
ObjectInputStream ois = new ObjectInputStream(gis);
//Now we can finally use it:
SomeObject someObject = (SomeObject)ois.readObject();
Thank You

More Related Content

PPS
PPTX
Temporal composability
PPT
Phani Kumar - Decorator Pattern
PPTX
Decorator Pattern 2.pptxdsadsadsadsadasdasdsa
PPTX
Design pattern reading group – decorator pattern
PDF
The Decorator Pattern
PPTX
Decorator Pattern
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
Temporal composability
Phani Kumar - Decorator Pattern
Decorator Pattern 2.pptxdsadsadsadsadasdasdsa
Design pattern reading group – decorator pattern
The Decorator Pattern
Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern

Similar to Decorator design pattern (20)

PPTX
Decorator Pattern presentation theory and examples
PDF
Decorator design pattern (A Gift Wrapper)
PPT
15 decorator pattern
PPTX
Decorator design pattern
PDF
Decorator Pattern | Object Oriented Design Pattern
PPTX
Decorator Design Pattern
PPTX
Decorator design pattern
PDF
Design pattern decorator pattern
DOCX
Automated Restaurant System (Command Design Pattern)PROBLEMY.docx
PPT
Design patterns - Decorator pattern
PPTX
Decorator Design Pattern
DOCX
1 – Implementing the Decorator Design Pattern (with St.docx
PPTX
Design Pattern(decorator design pattern) and anti pattern
PPTX
java training in chennai
PDF
Java design patterns
PDF
Design patterns 1july
PPTX
Design pattern part 2 - structural pattern
PDF
Design patterns in XPages
PDF
JS Fest 2019/Autumn. Daniel Ostrovsky. Falling in love with decorators ES6/Ty...
PDF
Webinar on Design Patterns titled 'Dive into Design Patterns'
Decorator Pattern presentation theory and examples
Decorator design pattern (A Gift Wrapper)
15 decorator pattern
Decorator design pattern
Decorator Pattern | Object Oriented Design Pattern
Decorator Design Pattern
Decorator design pattern
Design pattern decorator pattern
Automated Restaurant System (Command Design Pattern)PROBLEMY.docx
Design patterns - Decorator pattern
Decorator Design Pattern
1 – Implementing the Decorator Design Pattern (with St.docx
Design Pattern(decorator design pattern) and anti pattern
java training in chennai
Java design patterns
Design patterns 1july
Design pattern part 2 - structural pattern
Design patterns in XPages
JS Fest 2019/Autumn. Daniel Ostrovsky. Falling in love with decorators ES6/Ty...
Webinar on Design Patterns titled 'Dive into Design Patterns'
Ad

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
AI in Product Development-omnex systems
PDF
Nekopoi APK 2025 free lastest update
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Essential Infomation Tech presentation.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Digital Strategies for Manufacturing Companies
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Introduction to Artificial Intelligence
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
AI in Product Development-omnex systems
Nekopoi APK 2025 free lastest update
VVF-Customer-Presentation2025-Ver1.9.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Essential Infomation Tech presentation.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
top salesforce developer skills in 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Digital Strategies for Manufacturing Companies
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
CHAPTER 2 - PM Management and IT Context
Introduction to Artificial Intelligence
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
Ad

Decorator design pattern

  • 1. Decorator Design Pattern Ahmad Alkhous Damascus University alkhousahmad@gmail.com
  • 2. decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class What is Decorator design pattern?
  • 3. What problems can the Decorator design pattern solve? • Responsibilities should be added to (and removed from) an object dynamically at run-time. • A flexible alternative to subclassing for extending functionality should be provided.
  • 4. What is the problem with subclassing ? • With number of various functionalities that can be mixed together Here comes the BIG problem
  • 5. Who is hungry? Beef Slice Bread Lettuce Cheddar Egg Mayo Ketchup
  • 6. How to calculate the price of the order Beef Slice Bread Lettuce Cheddar Egg Mayo Ketchup 5 10 3 2 5 1 1
  • 7. How to calculate the price of the order
  • 8. How to calculate the price of the order interface Icalculate { public double calculate(); }
  • 9. How to calculate the price of the order interface Icalculate { public double calculate(); } class BeefMeal:Icalculate{ .. .. public double calculate() { return 10; } .. }
  • 10. How to calculate the price of the order interface Icalculate { public double calculate(); } class BeefMeal:Icalculate{ .. .. public double calculate() { return 10; } .. } class BeefMealWithLettuce : Icalculate { .. .. public double calulate() { return 10 +3 ; } }
  • 12. Our Burger Meal revisited Demo
  • 13. So Where is the Trick ?
  • 14. So Where is the Trick ?
  • 15. So Where is the Trick ? • This allows multiple decorators to be stacked on top of each other, each time adding a new functionality to the overridden method(s)
  • 16. Another Famous Example – Java IO //First open an inputstream of it: FileInputStream fis = new FileInputStream("/objects.gz"); //We want speed, so let's buffer it in memory: BufferedInputStream bis = new BufferedInputStream(fis); //The file is gzipped, so we need to ungzip it: GzipInputStream gis = new GzipInputStream(bis); //We need to unserialize those Java objects: ObjectInputStream ois = new ObjectInputStream(gis); //Now we can finally use it: SomeObject someObject = (SomeObject)ois.readObject();