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

SOLID principles

Tien Le
November 06, 2013

SOLID principles

The principles when applied together intend to create a system
that is easy to maintain and extend over time.
It is typically used with test-driven development,
and is part of an overall strategy of agile and adaptive programming.

Tien Le

November 06, 2013
Tweet

More Decks by Tien Le

Other Decks in Programming

Transcript

  1. The principles when applied together intend to create a system

    The principles when applied together intend to create a system that is easy to maintain and extend over time. that is easy to maintain and extend over time. It is typically used with test-driven development, It is typically used with test-driven development, and is part of an overall strategy of agile and adaptive programming. and is part of an overall strategy of agile and adaptive programming. Wikipedia.org Wikipedia.org
  2. Single Responsibility Principle Single Responsibility Principle “ “A class should

    have one, and only one, A class should have one, and only one, reason to change (responsibility) reason to change (responsibility)” ” - Uncle Bob - Uncle Bob “ “do one thing well” - Unix philosophy do one thing well” - Unix philosophy
  3. class class Scraper Scraper def initialize def initialize end end

    // input // input def fetch_data def fetch_data end end // process // process def parse def parse End End // ouput // ouput def to_s def to_s End End // store // store def save def save end end end end class class Scraper Scraper include Scraper::Fetchable include Scraper::Fetchable include Scraper::Parsable include Scraper::Parsable include Scraper::Printable include Scraper::Printable Include Scraper::Persistable Include Scraper::Persistable def initialize; end def initialize; end end end
  4. Open/Closed Principle Open/Closed Principle “ “You should be able to

    extend a class behavior You should be able to extend a class behavior without modifying it” without modifying it” - Uncle Bob - Uncle Bob
  5. Open/Closed Principle Open/Closed Principle "Favor ' "Favor 'object composition object

    composition' over ' ' over 'class inheritance'. class inheritance'." " (Gang of Four 1995:20) (Gang of Four 1995:20) E.g: Rspec matchers, ActiveRecord validators, … E.g: Rspec matchers, ActiveRecord validators, … class Person class Person Include ActiveModel::Validation Include ActiveModel::Validation validates_with validates_with HasNameValidator HasNameValidator End End RSpec::Matchers RSpec::Matchers.define :be_a_multiple_of do |expected| .define :be_a_multiple_of do |expected| match do |actual| match do |actual| actual % expected == 0 actual % expected == 0 end end end end expect(9).to be_a_multiple_of(3) expect(9).to be_a_multiple_of(3)
  6. Liskov Subtitution Principle Liskov Subtitution Principle “ “Derived classes must

    be substitutable Derived classes must be substitutable for their base class” for their base class” - Uncle Bob - Uncle Bob
  7. Interface Segregation Principle Interface Segregation Principle “ “Many client specific

    interfaces Many client specific interfaces are better than one general purpose interface” are better than one general purpose interface”
  8. Dependency Inversion Principle Dependency Inversion Principle “ “Depend on abstractions,

    not on concretions” Depend on abstractions, not on concretions” - Uncle Bob - Uncle Bob
  9. class Scraper class Scraper def to_s def to_s PrettyPrinter PrettyPrinter.print(self)

    .print(self) end end End End class Scraper class Scraper def to_s(printer = def to_s(printer = PrettyPrinter PrettyPrinter) ) printer printer.print(self) .print(self) end end end end
  10. Some stuffs in this slide are from: Agile Software Development,

    Principles, Patterns, and Practices – Rober C. Martin aka “Uncle Bob” https://speakerdeck.com/plataformatec/rails-solid-by-jose-valim-railsconf-2011 Thank you Happy coding ;-) Thank you Happy coding ;-)