Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SOLID Design Principles in Ruby

SOLID Design Principles in Ruby

Slides of my talk on 'SOLID Design Principles in Ruby' at RedDot Ruby Conf Singapore.

anildigital

June 27, 2014
Tweet

More Decks by anildigital

Other Decks in Programming

Transcript

  1. Rigid Difficult to change. (Every change causes too many changes

    in other parts of the system) Design smells
  2. • Loosely Coupled - Dependency Injection • Highly Cohesive -

    Single Responsibility Why SOLID? It helps us to write code which is
  3. • Loosely Coupled - Dependency Injection • Highly Cohesive -

    Single Responsibility • Easily Composable - Can be changed Why SOLID? It helps us to write code which is
  4. • 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
  5. • 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
  6. • Loosely Coupled - Dependency Injection • Highly Cohesive -

    Single Responsibility • Easily Composable - Can be changed • Context Independent - Can be rearranged • Reusable • Easily testable Why SOLID? It helps us to write code which is
  7. • Easy to maintain and extend over time Why SOLID?

    Ultimately it helps us to write code which is
  8. Single Responsibility ! • A class should serve a single

    purpose
 • There should never be more than one reason for a class to change.
  9. Single Responsibility • Reveal intent • Rename • Introduce constant

    • Introduce variable
 • Extract ‘Til you drop http://j.mp/extractDrop Achieve using following techniques
  10. Open/Closed • Replace branching with delegation. • Inject behaviour •

    Extract method • Extract class • Wrap behaviour with abstraction
  11. Parser + parse(xml) RSS Parser Atom Parser parse parse has

    been closed for modification Open/Closed Abstraction
  12. Subtypes must be substitutable for their base types. 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
  13. If a piece of client code works for a type

    then it must work for all derived/ variant types. 
 A new subtype should not screw up the client code. Liskov Substitution
  14. • Implement inheritance based on behaviour. • Do not violate

    behaviour of parent class
 • Obey the pre and postconditions rules. Liskov Substitution Rules
  15. Code example. Liskov Substitution Lets see classic Rectangle, Square problem

    Also we will see what are preconditions and postconditions rules
  16. Clients should not be forced to depend on methods they

    do not use. Interface Segregation
  17. Code example. Interface Segregation Lets see HDTV, Normal TV app

    example And car, driver, mechanic app example
  18. Dependency Inversion Abstractions should not depend on details. Details should

    depend on abstractions. High-level modules should not depend on low-level modules. 
 Both should depend on abstractions.
  19. • Rigid code —> Flexible code • Fragile code —>

    Robust code • Scary code —> Cuddly code Where does applying SOLID lead?
  20. Design because TDD is not enough DRY is not enough

    Design because you expect your application to succeed (and to change in the future to come)
  21. • Design principles — Set of guidelines • Design patterns

    — Reusable solution to commonly occurring problems Design Principles vs Design Patterns
  22. 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 http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) References
  23. Q&A