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

LSP

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Avatar for ilumin ilumin
February 11, 2015

 LSP

Avatar for ilumin

ilumin

February 11, 2015

More Decks by ilumin

Other Decks in Programming

Transcript

  1. • Child  classes  should  never  break  the  parent   class'

     type  definitions.   • Subtypes  must  be  substitutable  for  their  base   types
  2. Who  is  Liskov? • Hipster!    of  Computer  science  She

     is  an  American  computer  scientist  / Professor  @MIT
  3. Conclusion The  Liskov  Substitution  Principle:   • gives  us  a

     way  to  characterise  good  inheritance   hierarchies,   • increases  our  awareness  about  traps  that  will  cause   us  to  create  hierarchies  that  do  not  conform  to  the   open-­‐closed  principle.
 (  This  principle  is  just  an  extension  of  the  Open   Close  Principle  and  it  means  that  we  must  make   sure  that  new  derived  classes  are  extending  the   base  classes  without  changing  their  behavior.  )
  4. More  Detail Liskov  Substitution  Principle  requires  that   • Preconditions

     cannot  be  strengthened  in  a  subtype.   • Postconditions  cannot  be  weakened  in  a  subtype.   • Invariants  of  the  super  type  must  be  preserved  in  a  subtype.   • History  constraint  (the  "history  rule").  Objects  are  regarded  as   being  modifiable  only  through  their  methods  (encapsulation).   Since  subtypes  may  introduce  methods  that  are  not  present  in   the  super  type,  the  introduction  of  these  methods  may  allow   state  changes  in  the  subtype  that  are  not  permissible  in  the   super  type.  The  history  constraint  prohibits  this.