SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Design Patterns Hands-On: Come aggiungere responsabilità e ruoli alle classi esistenti senza modificarle Domenico Ventura Il Decorator Pattern
chi era costui? Design Patterns
Un Design Pattern... ,[object Object],[object Object],[object Object]
No code reuse... ,[object Object],[object Object]
Design Patterns: storia e eventi ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Il DECORATOR pattern Design Patterns Hands-on:
Jug Torino Lounge Bar ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Le cose perbene: UML! TableOrder + totalCost() + print() + addCocktail() Has (1-n) Cocktail <<interface>> +  cost() + description() Daiquiri Martini TequilaSunrise Caipirinha
Un caso di test ,[object Object],Tavolo 12 2 daiquiri 9,00 + 1 martini 3,70 + 1 tequila  Sr. 5,20  TOT.  17,90
Marketing: cocktail specials! ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Marketing: cocktail specials! ,[object Object],[object Object],[object Object],[object Object],[object Object]
Ecco il primo tentativo Una vera esplosione di classi! Problemi derivanti? Cocktail <<interface>> + cost() + description() Daiquiri Martini TequilaSunrise Caipirinha DoubleMartini MartiniOtr AbsolutMartini AppetizerMartini DaiquiriOtr AbsolutDaiquiri AppetizerDaiquiri
Riproviamo... ,[object Object],[object Object]
Di nuovo UML! TableOrder + totalCost() + print() + addCocktail() Has (1-n) Cocktail <<interface>> + cost() + description() BaseCocktail + isAbsolut() + setAbsolut(); + ... + cost() + description() Daiquiri Martini TequilaSunrise Caipirinha
Un altro caso di test ,[object Object],Tavolo 8 1 Absolut Martini Otr (3,7+1,2+0,8)   5,70 + 2 Teq.Sr con appetizers 2*(5,20 + 2,20) 14,80 + 1 Caipirinha   4,80  TOT.  25,30
WOW! I test passano! ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ancora quelli del Marketing! ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
E il Double Cocktail? ??
O. C. P. ,[object Object],[object Object]
Il DECORATOR pattern ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Decoriamo? Delega? ,[object Object],Absolut  è un decorator, che fa da wrapper all’oggetto che decora (sfruttando il polimorfismo) OnTheRocks Absolut Martini
E il metodo cost()? 3,7 0.8 1.2 Chiamata a cost() sul decorator esterno   Chiamata a cost sull’oggetto decorato Chiamata a cost sull’oggetto decorato €  4.7 cost() OnTheRocks cost() Absolut Martini cost()
Caso di test con double ,[object Object],Tavolo 4 1 Double Absolut Martini 1.8*(3,7+1,2)   8,82 + 2 Daiquiri con appetizers 2*(4,50 + 2,20) 13,40 + 1 Double Caipirinha 1.8*4,80   8,64  TOT.  30,86
Decorator: cosa sappiamo ,[object Object],[object Object],[object Object]
Decorator: definizione UML Component + aMethod() ConcreteDecorator + aMethod() Component ConcreteComponent + aMethod() Decorator +aMethod() - Component decorated
Un esempio celebre ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Un’altro esempio
Filtri sulle righe? ,[object Object],[object Object],[object Object]
Decorator e conti correnti LineDetector + detect() Component SimpleDetector + detect() DateDetector +detect() - LineDetector decorated MovementDetector +detect() - LineDetector decorated
Concludendo: decorator ,[object Object],[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Empfohlen

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 

Empfohlen (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

JUGTo Meeting febbario: Decorator Pattern

  • 1. Design Patterns Hands-On: Come aggiungere responsabilità e ruoli alle classi esistenti senza modificarle Domenico Ventura Il Decorator Pattern
  • 2. chi era costui? Design Patterns
  • 3.
  • 4.
  • 5.
  • 6. Il DECORATOR pattern Design Patterns Hands-on:
  • 7.
  • 8. Le cose perbene: UML! TableOrder + totalCost() + print() + addCocktail() Has (1-n) Cocktail <<interface>> + cost() + description() Daiquiri Martini TequilaSunrise Caipirinha
  • 9.
  • 10.
  • 11.
  • 12. Ecco il primo tentativo Una vera esplosione di classi! Problemi derivanti? Cocktail <<interface>> + cost() + description() Daiquiri Martini TequilaSunrise Caipirinha DoubleMartini MartiniOtr AbsolutMartini AppetizerMartini DaiquiriOtr AbsolutDaiquiri AppetizerDaiquiri
  • 13.
  • 14. Di nuovo UML! TableOrder + totalCost() + print() + addCocktail() Has (1-n) Cocktail <<interface>> + cost() + description() BaseCocktail + isAbsolut() + setAbsolut(); + ... + cost() + description() Daiquiri Martini TequilaSunrise Caipirinha
  • 15.
  • 16.
  • 17.
  • 18. E il Double Cocktail? ??
  • 19.
  • 20.
  • 21.
  • 22. E il metodo cost()? 3,7 0.8 1.2 Chiamata a cost() sul decorator esterno Chiamata a cost sull’oggetto decorato Chiamata a cost sull’oggetto decorato € 4.7 cost() OnTheRocks cost() Absolut Martini cost()
  • 23.
  • 24.
  • 25. Decorator: definizione UML Component + aMethod() ConcreteDecorator + aMethod() Component ConcreteComponent + aMethod() Decorator +aMethod() - Component decorated
  • 26.
  • 28.
  • 29. Decorator e conti correnti LineDetector + detect() Component SimpleDetector + detect() DateDetector +detect() - LineDetector decorated MovementDetector +detect() - LineDetector decorated
  • 30.