SlideShare a Scribd company logo
1 of 10
Decorator Design
Pattern
Elements of Reusable Object-Oriented Software
Content
1. Design Patterns
2. Decorator Design Pattern
3. Example
I. Class Structure
II. Implementation
4. Conclusion
2
Design Pattern
3
Best practices Solutions to
general problems
Decorator Design Pattern
• Attach additional responsibilities to an object dynamically.
• Decorators provide a flexible alternative to subclassing for
extending functionality.
• Wrapping a gift, putting it in a box, and wrapping the box.
4
Problem
You want to add behavior or state to individual objects at runtime.
Inheritance is not feasible because it is static and applies to an
entire class.
5
Example
6
Window
Window with Vertical
Scrollbar
Window with Horizontal
Scrollbar
Window with Border
Window with Vertical and Horizontal Scrollbar
Window with Vertical and Horizontal Scrollbar and Border
Class Structure
7
<<interface>>
Window
+ draw()
<<class>>
MainWindow
+ draw()
<<interface>>
WindowDecorator
+ draw()
<<class>>
Border
<<class>>
Vertical SB
<<class>>
Horizontal SB
Implementation
8
Window appMainWindow = new BorderDecorator(
new HorizontalScrollBarDecorator(
new VerticalScrollBarDecorator(
new MainWindow( 80, 24 ))));
appMainWindow.draw();
Conclusion
• Decorators allow behavior modification at runtime rather than
going back into existing code and making changes
• The decorator pattern supports the principle that classes should
be open for extension but closed for modification
9
Kanushka Gayan
SE intern @ Extrogene Software (Pvt) Ltd
kanushkanet@gmail.com
071 879 4546
Thank You!

More Related Content

Similar to Decorator Design Pattern

Web Design Syllabus.doc
Web Design Syllabus.docWeb Design Syllabus.doc
Web Design Syllabus.doc
butest
 
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
chetankane
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 

Similar to Decorator Design Pattern (20)

cis 510 Imagine Your Future/newtonhelp.com   
cis 510 Imagine Your Future/newtonhelp.com   cis 510 Imagine Your Future/newtonhelp.com   
cis 510 Imagine Your Future/newtonhelp.com   
 
cis 510 Life of the Mind/newtonhelp.com   
cis 510 Life of the Mind/newtonhelp.com   cis 510 Life of the Mind/newtonhelp.com   
cis 510 Life of the Mind/newtonhelp.com   
 
cis 510 Focus Dreams/newtonhelp.com
cis 510 Focus Dreams/newtonhelp.comcis 510 Focus Dreams/newtonhelp.com
cis 510 Focus Dreams/newtonhelp.com
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design patterns: Understand the patterns and design your own
Design patterns: Understand the patterns and design your ownDesign patterns: Understand the patterns and design your own
Design patterns: Understand the patterns and design your own
 
Web Design Syllabus.doc
Web Design Syllabus.docWeb Design Syllabus.doc
Web Design Syllabus.doc
 
CIS 510 Inspiring Innovation/tutorialrank.com
 CIS 510 Inspiring Innovation/tutorialrank.com CIS 510 Inspiring Innovation/tutorialrank.com
CIS 510 Inspiring Innovation/tutorialrank.com
 
CIS 532 Effective Communication/tutorialrank.com
 CIS 532 Effective Communication/tutorialrank.com CIS 532 Effective Communication/tutorialrank.com
CIS 532 Effective Communication/tutorialrank.com
 
Software Development: Beyond Training wheels
Software Development: Beyond Training wheelsSoftware Development: Beyond Training wheels
Software Development: Beyond Training wheels
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
CIS 331 Effective Communication/tutorialrank.com
 CIS 331 Effective Communication/tutorialrank.com CIS 331 Effective Communication/tutorialrank.com
CIS 331 Effective Communication/tutorialrank.com
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
DP PPTS by BK.pptx
DP PPTS by BK.pptxDP PPTS by BK.pptx
DP PPTS by BK.pptx
 
Design pattern
Design patternDesign pattern
Design pattern
 
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
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 

More from Kanushka Gayan (6)

Progressive Web Apps
Progressive Web AppsProgressive Web Apps
Progressive Web Apps
 
Microservices Architecture
Microservices ArchitectureMicroservices Architecture
Microservices Architecture
 
Git
GitGit
Git
 
Slim 3 PHP micro framework
Slim 3 PHP micro frameworkSlim 3 PHP micro framework
Slim 3 PHP micro framework
 
REST API
REST APIREST API
REST API
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Decorator Design Pattern

Editor's Notes

  1. https://sourcemaking.com/design_patterns/decorator
  2. Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class. This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
  3. Suppose you are working on a user interface toolkit and you wish to support adding borders and scroll bars to windows. You could define an inheritance hierarchy like But the Decorator pattern suggests giving the client the ability to specify whatever combination of "features" is desired.
  4. Suppose you are working on a user interface toolkit and you wish to support adding borders and scroll bars to windows. You could define an inheritance hierarchy like But the Decorator pattern suggests giving the client the ability to specify whatever combination of "features" is desired.