Omg! Patterns by Max Titov

Post on 24-May-2015

248 views 2 download

Tags:

description

Learn Design Patterns fun by code.

Transcript of Omg! Patterns by Max Titov

By Max Titovmaxtitov.me

Ninja Software Operations

OMG! Patterns. Decorator

OMG! Patterns?

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

Example

Beauty salon.

Salon provides following services:▶ Haircut▶ Coloring▶ Styling

Salon business

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

Client: Right?

F@#k

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 FrameworksBaseElement

ScrollbarBehavior(BaseElement)

ShadowBehavior(BaseElement)▶ Streams

FileStreamGZipStream(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 PatternsBy Eric Freeman

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

▶Search Google for: Design patterns

What next?

More patterns!

Questions?OMG! Patterns. Decorator

By Max TitovGet examples: www.maxtitov.me

https://github.com/eolexeGet in touch: eolexe@gmail.com

Twitter: eolexe