SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à Omg! Patterns <decorator> by Max Titov

Similaire à 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
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
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