omg! patterns by max titov

30
By Max Titov maxtitov.me Ninja Software Operations OMG! Patterns. Decorator

Upload: max-titov

Post on 24-May-2015

249 views

Category:

Technology


2 download

DESCRIPTION

Learn Design Patterns fun by code.

TRANSCRIPT

Page 1: Omg! Patterns  by Max Titov

By Max Titovmaxtitov.me

Ninja Software Operations

OMG! Patterns. Decorator

Page 2: Omg! Patterns  by Max Titov

OMG! Patterns?

▶ About mostly used patterns▶ Examples of usage in real life▶ Learning by code examples▶ Learning new language

Page 3: Omg! Patterns  by Max Titov

Example

Beauty salon.

Salon provides following services:▶ Haircut▶ Coloring▶ Styling

Salon business

Page 4: Omg! Patterns  by Max Titov

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

Page 5: Omg! Patterns  by Max Titov
Page 6: Omg! Patterns  by Max Titov

System Overview

class Salon{

string HaircutDescription();string StylingDescription();string ColoringDescription();

}

Page 7: Omg! Patterns  by Max Titov

Don’t you feel like?

WHY?

Page 8: Omg! Patterns  by Max Titov

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▶ #@#$&%%!!!

Page 9: Omg! Patterns  by Max Titov

Lets do a bit of refactoring

▶ Identify common interface (if any)▶ Apply single responsibility principle▶ Use of old good inheritance

Page 10: Omg! Patterns  by Max Titov

System Overview

interface ISalonService{

string getDescription();}

Page 11: Omg! Patterns  by Max Titov

System Overview

class Haircut: ISalonService{

string getDescription(){

return ‘Haircut’;}

}

Page 12: Omg! Patterns  by Max Titov

System Overview

Page 13: Omg! Patterns  by Max Titov

Muuuuuch better!

Page 14: Omg! Patterns  by Max Titov

Adding new functionality

Page 15: Omg! Patterns  by Max Titov

Requirements update

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

Page 16: Omg! Patterns  by Max Titov

Requirements update

Client: Right?

F@#k

Page 17: Omg! Patterns  by Max Titov

Identifying the problems

▶ Requires new class for each combination of services

▶ Hard to extend and maintain

Page 18: Omg! Patterns  by Max Titov

Decorator

▶ The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality

Page 19: Omg! Patterns  by Max Titov

Decorator UML

Page 20: Omg! Patterns  by Max Titov

Abstract Decorator

class abstract SalonServiceDecorator: ISalonService{

SalonServiceDecorator (ISalonService service);

string getDescription();}

Page 21: Omg! Patterns  by Max Titov

Concrete Decorator

class HaircutDecorator{

HaircutDecorator (ISalonService service);

string getDescription();}

Page 22: Omg! Patterns  by Max Titov

Use of Decorators

salonService = new SalonService();salonService = new HaircutDecorator(salonService)salonService = new StylingDecorator(salonService)salonService.getDescription()

Output:> Salon service: Haircut, Styling

Page 23: Omg! Patterns  by Max Titov

System Overview

Page 24: Omg! Patterns  by Max Titov

Success!

Page 25: Omg! Patterns  by Max Titov

Code Time!

Page 26: Omg! Patterns  by Max Titov

Where Used

▶ GUI FrameworksBaseElement

ScrollbarBehavior(BaseElement)

ShadowBehavior(BaseElement)▶ Streams

FileStreamGZipStream(ByteStream)

Page 27: Omg! Patterns  by Max Titov

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)

Page 28: Omg! Patterns  by Max Titov

Books

▶Head First Design PatternsBy Eric Freeman

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

▶Search Google for: Design patterns

Page 29: Omg! Patterns  by Max Titov

What next?

More patterns!

Page 30: Omg! Patterns  by Max Titov

Questions?OMG! Patterns. Decorator

By Max TitovGet examples: www.maxtitov.me

https://github.com/eolexeGet in touch: [email protected]

Twitter: eolexe