OO Design





Inspired by Sandi Metz's "POODR"

What Is design?




MAINTAINABILITY

What it means?




  • No Side Effects
  • Easy to reuse
  • Product change is proportional to code change





Single responsibility

principle

SRP



SRP


Helps you deciding what belongs in a class


  • Mr Gear, what is your ratio?
  • Mr Gear, what is your tire?

SRP


Why it matters ?

  • Helps reusability



Without it

  • Can't reuse
  • Leads to duplication





interfaces

interfaces


Public Interfaces

  • Reveal responsibility
  • Expect to be used by others
  • Will not change
  • Documented

interfaces


Private interfaces

  • Implementation details
  • Not expected to be used by others
  • May change

interfaces


Interfaces


interfaces


interfaces


Fig 4.5
  • Procedural programming
  • Not Object-Oriented


Fig 4.6
  • Less public interface
  • Less chance to change


Fig 4.7
  • Switch from how to what
  • U can extend 'Trip' class without touching it!






Law of demeter

LOD




"Only talk to your neighbors"
(aka "Only use one dot") 

LOD


Some code in Trip#depart

  • customer.bicycle.wheel.rotate
  • customer.bicycle.wheel.tire
  • hash.keys.sort.map(&:to_s)

LOD




  • If Wheel#tire change
  • It may break Trip#depart
  • This is unexpected side effect

lod



Only a "law" because someone name it a "law"

  • customer.bicycle.wheel.rotate - BAD
  • customer.bicycle.wheel.tire - I DUNNO
  • hash.keys.sort.map(&:to_s) - OK







coding for fun

hide instance variables



Using accessors

hide data structures


Too much knowing about the @data structure


HIDE DATA STRUCTURES


Using Struct

extract responsibilities


One method

Becomes two

isolate responsibilities


Also using Struct





Q&A

Made with Slides.com