SlideShare una empresa de Scribd logo
1 de 30
OMG! Patterns.
Decorator

                       By Max Titov
                        maxtitov.me
          Ninja Software Operations
OMG! Patterns?
▶   About mostly used patterns
▶   Examples of usage in real life
▶   Learning by code examples
▶   Learning new language
Example
Beauty salon.



                  Salon business

Salon provides following services:
  ▶   Haircut
  ▶   Coloring
  ▶   Styling
The Objective
▶   Salon going to add list of new services
▶   Salon want to have different combinations
    of services in their pricelist
▶   Salon want’s to expand for new market
    and provide services for males
System Overview
class Salon
{
   string HaircutDescription();
   string StylingDescription();
   string ColoringDescription();
}
Don’t you feel like?




    WHY?
Identifying the problems
▶   There is only one thing wrong with this
    approach: everything.
    ▶   God class. Single responsibility principle?
        Never heard about…
    ▶   Code duplication
    ▶   Hard to test
    ▶   #@#$&%%!!!
Lets do a bit of refactoring
▶   Identify common interface (if any)
▶   Apply single responsibility principle
▶   Use of old good inheritance
System Overview
interface ISalonService
{
   string getDescription();
}
System Overview
class Haircut: ISalonService
{
   string getDescription()
   {
       return ‘Haircut’;
   }
}
System Overview
Muuuuuch better!
Adding new functionality
Requirements update




Client: Hey, we want to add two
other services and remove one.
That wouldn’t take much time
right?
Requirements update
                             F@#k




Client: Right?
Identifying the problems
▶   Requires new class for each combination
    of services
▶   Hard to extend and maintain
Decorator
▶   The Decorator Pattern attaches additional
    responsibilities to an object dynamically.
    Decorators provide a flexible alternative to
    subclassing for extending functionality
Decorator UML
Abstract Decorator
class abstract SalonServiceDecorator:
ISalonService
{
   SalonServiceDecorator (ISalonService service);
   string getDescription();
}
Concrete Decorator
class HaircutDecorator
{
   HaircutDecorator (ISalonService service);
   string getDescription();
}
Use of Decorators
salonService = new SalonService();
salonService = new
HaircutDecorator(salonService)
salonService = new
StylingDecorator(salonService)
salonService.getDescription()

Output:
> Salon service: Haircut, Styling
System Overview
Success!
Code Time!
Where Used
▶   GUI Frameworks
       BaseElement
          ScrollbarBehavior(BaseElement)
          ShadowBehavior(BaseElement)
▶   Streams
       FileStream
          GZipStream(ByteStream)
Pros and Cons
Pros:
+ The cure for ugly code
+ Composition over inheritance (Adds flexibility)
+ Add behavior at runtime without class modification
+ Behavior can be reused

Cons:
– Too many small classes
– Could be not obvious for other developers
(example: Java streams)
Books
▶   Head First Design Patterns
By Eric Freeman

▶Design Patterns : Elements of
Reusable Object-Oriented Software
By (GoF) Gamma, Helm, Johnson, Vlissides

▶   Search Google for: Design patterns
What next?

More patterns!
      
Questions?
  OMG! Patterns. Decorator
          By Max Titov
Get examples: www.maxtitov.me
   https://github.com/eolexe
Get in touch: eolexe@gmail.com
         Twitter: eolexe

Más contenido relacionado

Similar a Omg! Patterns <decorator> by Max Titov

Similar a Omg! Patterns <decorator> by Max Titov (20)

Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injection
 
Pega Senior System Architect (CPSSA) Exam - Questions & Answers.pdf
Pega Senior System Architect (CPSSA) Exam - Questions & Answers.pdfPega Senior System Architect (CPSSA) Exam - Questions & Answers.pdf
Pega Senior System Architect (CPSSA) Exam - Questions & Answers.pdf
 
C Sharp Certification
C Sharp CertificationC Sharp Certification
C Sharp Certification
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
 
Understanding computer vision with Deep Learning
Understanding computer vision with Deep LearningUnderstanding computer vision with Deep Learning
Understanding computer vision with Deep Learning
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
Code Metrics
Code MetricsCode Metrics
Code Metrics
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Lec18
Lec18Lec18
Lec18
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
[Sample Question] SAFe Agile Software Engineer (ASE) Exam
[Sample Question] SAFe Agile Software Engineer (ASE) Exam[Sample Question] SAFe Agile Software Engineer (ASE) Exam
[Sample Question] SAFe Agile Software Engineer (ASE) Exam
 
Microservices Chaos Testing at Jet
Microservices Chaos Testing at JetMicroservices Chaos Testing at Jet
Microservices Chaos Testing at Jet
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
 
Journey's diary developing a framework using tdd
Journey's diary   developing a framework using tddJourney's diary   developing a framework using tdd
Journey's diary developing a framework using tdd
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Omg! Patterns <decorator> by Max Titov