solid design principles in ruby

79
SOLID Anil Wadghule Software Engineer, Equal Experts [email protected] Design principles in Ruby

Upload: anil-wadghule

Post on 10-May-2015

1.153 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: SOLID design principles in Ruby

SOLID

Anil Wadghule Software Engineer, Equal Experts [email protected]

Design principles in Ruby

Page 2: SOLID design principles in Ruby
Page 3: SOLID design principles in Ruby

In the beginning your application was perfect

Page 4: SOLID design principles in Ruby

Your application will change.

Page 5: SOLID design principles in Ruby
Page 6: SOLID design principles in Ruby

Then it has changed.

Page 7: SOLID design principles in Ruby

Modular code doesn’t mean good design

Page 8: SOLID design principles in Ruby

Design is all about managing dependencies.

Page 9: SOLID design principles in Ruby

Dependencies are important

Z

A

X

YA

X

Y

Z

A

B

class

subclass

Page 10: SOLID design principles in Ruby

Design might save you.

Unmanaged dependencies are killing your application

Page 11: SOLID design principles in Ruby

What are smells of bad design?

Page 12: SOLID design principles in Ruby

Design smells

Page 13: SOLID design principles in Ruby

Design smells

Rigid

Page 14: SOLID design principles in Ruby

Rigid Difficult to change.

(Every change causes too many changes in other

parts of the system)

Design smells

Page 15: SOLID design principles in Ruby

Design smells

Page 16: SOLID design principles in Ruby

Design smells

Fragile

Page 17: SOLID design principles in Ruby

Fragile Easily breakable

(Each change breaks distant and unrelated

things)

Design smells

Page 18: SOLID design principles in Ruby

Design smells

Page 19: SOLID design principles in Ruby

Design smells

Immobile

Page 20: SOLID design principles in Ruby

Design smells

Immobile Reuse is impossible

(The code is hopelessly entangled)

Page 21: SOLID design principles in Ruby

Design smells

Page 22: SOLID design principles in Ruby

Design smells

Viscous

Page 23: SOLID design principles in Ruby

Design smells

Viscous Toughness in preserving

design (Doing things right is

harder than doing things wrong

Page 24: SOLID design principles in Ruby

It did not start that way.

Page 25: SOLID design principles in Ruby
Page 26: SOLID design principles in Ruby

Why SOLID?It helps us to write code which is

Page 27: SOLID design principles in Ruby

• Loosely Coupled - Dependency Injection

Why SOLID?It helps us to write code which is

Page 28: SOLID design principles in Ruby

• Loosely Coupled - Dependency Injection

• Highly Cohesive - Single Responsibility

Why SOLID?It helps us to write code which is

Page 29: SOLID design principles in Ruby

• Loosely Coupled - Dependency Injection

• Highly Cohesive - Single Responsibility • Easily Composable - Can be changed

Why SOLID?It helps us to write code which is

Page 30: SOLID design principles in Ruby

• Loosely Coupled - Dependency Injection

• Highly Cohesive - Single Responsibility • Easily Composable - Can be changed

• Context Independent - Can be

rearranged

Why SOLID?It helps us to write code which is

Page 31: SOLID design principles in Ruby

• Loosely Coupled - Dependency Injection

• Highly Cohesive - Single Responsibility • Easily Composable - Can be changed

• Context Independent - Can be

rearranged

• Reusable

Why SOLID?It helps us to write code which is

Page 32: SOLID design principles in Ruby

• Loosely Coupled - Dependency Injection

• Highly Cohesive - Single Responsibility • Easily Composable - Can be changed

• Context Independent - Can be rearranged

• Reusable • Easily testable code

Why SOLID?It helps us to write code which is

Page 33: SOLID design principles in Ruby

Robert Martin http://www.objectmentor.com

Page 34: SOLID design principles in Ruby

S O L I D

Principles

Page 35: SOLID design principles in Ruby

Single Responsibility O L I D

Principles

Page 36: SOLID design principles in Ruby

Single Responsibility Open/Closed L I D

Principles

Page 37: SOLID design principles in Ruby

Single Responsibility Open/Closed Liskov Substitution I D

Principles

Page 38: SOLID design principles in Ruby

Single Responsibility Open/Closed Liskov Substitution Interface Segregation D

Principles

Page 39: SOLID design principles in Ruby

Single Responsibility Open/Closed Liskov Substitution Interface Segregation Dependency Inversion

Principles

Page 40: SOLID design principles in Ruby

Lets look at these principles in detail.

Principles

Page 41: SOLID design principles in Ruby

Single Responsibility

Page 42: SOLID design principles in Ruby

Single Responsibility

!

• A class should serve a single purpose

Page 43: SOLID design principles in Ruby

Single Responsibility!

• A class should serve a single purpose

• There should never be more than one reason for a class to change.

Page 44: SOLID design principles in Ruby

Single Responsibility

• Generates ‘Highly cohesive’ code.

• Removes ‘Immobility Smell’

Page 45: SOLID design principles in Ruby

Code example.

Single Responsibility

Requirement: Client needs Feed saver application

Page 46: SOLID design principles in Ruby

Open/Closed

Page 47: SOLID design principles in Ruby

Software entities (classes/modules, methods) should be open for extension, but closed for modification

Open/Closed

Page 48: SOLID design principles in Ruby

Open/Closed• Helps to remove snake of ‘if-

else’ cases. • and remove those bad switch

cases.

Page 49: SOLID design principles in Ruby

Code example.

Open/Closed

Requirement: Client says application should save Atom feeds

Page 50: SOLID design principles in Ruby

Parser

+ parse(xml)

RSS Parser

Atom Parser

parse

parse has been closed for modification

Open/Closed

Page 51: SOLID design principles in Ruby

Liskov Substitution

Page 52: SOLID design principles in Ruby

Subclasses should be substitutable for their base classes.

Liskov Substitution

Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is subtype of T

Page 53: SOLID design principles in Ruby

If a piece of client code works for a type then it must work for all derived types. A new subtype should not screw up the client code.

Liskov Substitution

Page 54: SOLID design principles in Ruby

• Implement inheritance based on behaviour.

• Obey the pre and postconditions rules.

Liskov Substitution

Rules

Page 55: SOLID design principles in Ruby

Code example.

Liskov Substitution

Lets see classic Rectangle, Square problem

Also preconditions and postconditions rules

Page 56: SOLID design principles in Ruby

Interface Segregation

Page 57: SOLID design principles in Ruby

Many client specific interfaces are better than one general purpose interface.

Interface Segregation

Page 58: SOLID design principles in Ruby

Many client specific interfaces are better than one general purpose interface.

Interface Segregation

Page 59: SOLID design principles in Ruby

Many client specific classes are better than one general purpose class.

Interface Segregation

Page 60: SOLID design principles in Ruby

Interface Segregation

• Helps for ‘Highly cohesive’ code.

• Removes ‘Immobility Smell’

Page 61: SOLID design principles in Ruby

Code example.

Interface Segregation

Lets see HDTV, Normal TV application

Also car, driver, mechanic app

Page 62: SOLID design principles in Ruby

Dependency Inversion

Page 63: SOLID design principles in Ruby

Depend on abstractions. Do not depend on concretions.

Dependency Inversion

A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.

Page 64: SOLID design principles in Ruby

Dependency InversionCopy

ReadKeyboard

char char

Dependencies downwards

WritePrinter

Dependent design

Page 65: SOLID design principles in Ruby

Copy

ReadKeyboard

char char

Dependent design

Dependencies downwards

Dependency Inversion

WriteDisk

Page 66: SOLID design principles in Ruby

Copy

Reader

Inverted Dependencies.

Dependency Inversion

ReadKeyboard

Writer

PrinterWriter

Page 67: SOLID design principles in Ruby

Code example.

Dependency Inversion

Lets see classic Button & Lamp application

Page 68: SOLID design principles in Ruby

Dependency Inversion

Downwards dependency

Button

Lamp

Dependency Inversion

Page 69: SOLID design principles in Ruby

Abstract Button

Button Client

Inverted Dependencies.

Dependency InversionButton

Lamp

Abstract

Details

Details

Page 70: SOLID design principles in Ruby
Page 71: SOLID design principles in Ruby

It’s possible to learn Software Design and aim for good Software Design

Page 72: SOLID design principles in Ruby

Design becauseTDD is not enough

DRY is not enough

Design because you expect your application to succeed (and to change in the future to come)

Page 73: SOLID design principles in Ruby

• Design principles — Set of guidelines

• Design patterns — Reusable solution to commonly occurring problems

Design Principles vs Design Patterns

Page 74: SOLID design principles in Ruby

Abstraction is the key.

Page 75: SOLID design principles in Ruby

Thank you !!!Follow me on Twitter

@anildigital

Page 76: SOLID design principles in Ruby

Recommended Read

Page 77: SOLID design principles in Ruby

http://www.flickr.com/photos/scjn/3586487445

http://www.flickr.com/photos/51241173@N03/8083645853

http://www.flickr.com/photos/wouterrietberg/12076192934

http://www.flickr.com/photos/clonedmilkmen/3604999084

http://lostechies.com/wp-content/uploads/2011/03/pablos_solid_ebook.pdf

Photo credits

Page 78: SOLID design principles in Ruby

Sandi Metz - SOLID Object-Oriented Design talk 2009

Clean Coders Videos - Uncle Bob Martin

SOLID Ruby - Jim Weirich - Ruby Conference 2009

https://github.com/kevinbuch/solid for examples (ISP, DIP)

Pablo's SOLID Software Development - http://lostechies.com/wp-content/uploads/2011/03/pablos_solid_ebook.pdf

http://blog.groupbuddies.com/posts/19-solid-principles-in-ruby for examples (ISP)

http://www.codeproject.com/Articles/613304/SOLID-Principles-The-Liskov-Principle-What-Why-and for examples (LSP)

References

Page 79: SOLID design principles in Ruby

Questions?