SlideShare una empresa de Scribd logo
1 de 15
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
LSP
Stéphane Ducasse --- 2005
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
A step deeper in Subtyping...
S.Ducasse 4
Liskov Substitution Principle (LSP)
if for each object o1 of type S there is another
object o2 of type T such that for all programs P
defined in terms of T, the behavior of P is
unchanged when o1 is substituted for o2, then S
is a subtype of T.
Barbara Liskov, "Data Abstraction and
Hierarchy," SIGPLAN Notices, 23,5 (May 1988)
S.Ducasse 5
A Simple Bag
We will define a Bag as an object that accepts two
messages: put and count
(send a-Bag 'put x)
puts an element x into the Bag, and
(send a-Bag 'count x)
gives the count of occurrences of x in the Bag (without
changing a-Bag's state).
S.Ducasse 6
A Simple Set
Likewise, a Set is defined as an object that accepts two
messages:
(send a-Set 'put x)
puts an element x into a-Set unless it was already there,
(send a-Set 'count x)
gives the count of occurrences of x in a-Set (which is
always either 0 or 1).
S.Ducasse 7
A Bag Function
(define (fnb bag)
(send bag 'put 5)
(send bag 'put 5)
(send bag 'count 5))
The behavior of this function can be summed as follows:
given a Bag, the function adds two elements into it and
returns
(+ 2 (send orig-bag 'count 5))
S.Ducasse 8
The problem
Technically you can pass to fnb a Set object as well. Just
as a Bag, a Set object accepts messages put and count.
However applying fnb to a Set object will break the
function's post-condition
Therefore, passing a set object where a bag was expected
changes behavior of some program. According to the
Liskov Substitution Principle (LSP), a Set is not
substitutable for a Bag -- a Set cannot be a subtype of a
Bag.
S.Ducasse 9
Another function
(define (fns set)
(send set 'put 5)
(send set 'count 5))
The behavior of this function is: given a Set, the function
adds an element into it and returns 1.
S.Ducasse 10
Problem two
If you pass to this function a bag (which -- just as a set --
replies to messages put and count), the function fns
may return a number greater than 1. This will break
fns's contract, which promised always to return 1.
S.Ducasse 11
Finally
Therefore, from the OO point of view, neither a Bag nor a
Set are a subtype of the other.
Bag and Set only appear similar. The interface or an
implementation of a Bag and a Set appear to invite
subclassing of a Set from a Bag (or vice versa). Doing so
however will violate the LSP -- and you have to brace for
very subtle errors.
Sets and Bags are very simple types, far simpler than the
ones you deal with in a production code. either. It's
manual work -- you have to see the problem
S.Ducasse 12
Watch out
Alas, LSP when considered from an OOP point of view is
undecidable.
You cannot count on a compiler for help in pointing out
an error. You cannot rely on regression tests
S.Ducasse 13
In C++
typedef int const * CollIterator; // Primitive but will do
class CBag {
public:
int size(void) const; // The number of elements in the
bag
virtual void put(const int elem); // Put an element into the bag
int count(const int elem) const; // Count the number of occurrences
// of a particular element in the bag
virtual bool del(const int elem); // Remove an element from the bag
// Return false if the element
// didn't exist
CollIterator begin(void) const; // Standard enumerator interface
CollIterator end(void) const;
CBag(void);
virtual CBag * clone(void) const; // Make a copy of the bag
private:
// implementation details elided
};
S.Ducasse 14
CSet
class CSet : public CBag {
public:
bool memberof(const int elem) const { return count(elem) > 0; }
// Overriding of CBag::put
void put(const int elem)
{ if(!memberof(elem)) CBag::put(elem); }
CSet * clone(void) const
{ CSet * new_set = new CSet(); *new_set += *this; return new_set; }
CSet(void) {}
};
S.Ducasse 15
Summary
Subtyping...

Más contenido relacionado

La actualidad más candente

17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlockVahid Heidari
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum
 
OOP for Hardware Verification--Demystified!
OOP for Hardware Verification--Demystified! OOP for Hardware Verification--Demystified!
OOP for Hardware Verification--Demystified! DVClub
 

La actualidad más candente (8)

Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)
 
17. thread and deadlock
17. thread and deadlock17. thread and deadlock
17. thread and deadlock
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
 
OOP for Hardware Verification--Demystified!
OOP for Hardware Verification--Demystified! OOP for Hardware Verification--Demystified!
OOP for Hardware Verification--Demystified!
 
2
22
2
 

Destacado (20)

Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
10 reflection
10 reflection10 reflection
10 reflection
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 

Similar a Stoop 416-lsp

Container Classes
Container ClassesContainer Classes
Container Classesadil raja
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Philip Schwarz
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxAssg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxfestockton
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: NotesRoberto Casadei
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: NotesRoberto Casadei
 
Writer Monad for logging execution of functions
Writer Monad for logging execution of functionsWriter Monad for logging execution of functions
Writer Monad for logging execution of functionsPhilip Schwarz
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3Shaili Choudhary
 
Integration by parts to solve it clearly
Integration by parts  to solve it clearlyIntegration by parts  to solve it clearly
Integration by parts to solve it clearlymuhammadalam77863
 
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014Matthias Noback
 

Similar a Stoop 416-lsp (20)

9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
packet destruction in NS2
packet destruction in NS2packet destruction in NS2
packet destruction in NS2
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 
Container Classes
Container ClassesContainer Classes
Container Classes
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxAssg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
 
RxJava@DAUG
RxJava@DAUGRxJava@DAUG
RxJava@DAUG
 
lec10.pdf
lec10.pdflec10.pdf
lec10.pdf
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Feedback Vertex Set
Feedback Vertex SetFeedback Vertex Set
Feedback Vertex Set
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
Writer Monad for logging execution of functions
Writer Monad for logging execution of functionsWriter Monad for logging execution of functions
Writer Monad for logging execution of functions
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Integration by parts to solve it clearly
Integration by parts  to solve it clearlyIntegration by parts  to solve it clearly
Integration by parts to solve it clearly
 
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
 
Basilisk Guide.pdf
Basilisk Guide.pdfBasilisk Guide.pdf
Basilisk Guide.pdf
 

Más de The World of Smalltalk (15)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Stoop 416-lsp

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ LSP Stéphane Ducasse --- 2005
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 A step deeper in Subtyping...
  • 4. S.Ducasse 4 Liskov Substitution Principle (LSP) if for each object o1 of type S there is another object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov, "Data Abstraction and Hierarchy," SIGPLAN Notices, 23,5 (May 1988)
  • 5. S.Ducasse 5 A Simple Bag We will define a Bag as an object that accepts two messages: put and count (send a-Bag 'put x) puts an element x into the Bag, and (send a-Bag 'count x) gives the count of occurrences of x in the Bag (without changing a-Bag's state).
  • 6. S.Ducasse 6 A Simple Set Likewise, a Set is defined as an object that accepts two messages: (send a-Set 'put x) puts an element x into a-Set unless it was already there, (send a-Set 'count x) gives the count of occurrences of x in a-Set (which is always either 0 or 1).
  • 7. S.Ducasse 7 A Bag Function (define (fnb bag) (send bag 'put 5) (send bag 'put 5) (send bag 'count 5)) The behavior of this function can be summed as follows: given a Bag, the function adds two elements into it and returns (+ 2 (send orig-bag 'count 5))
  • 8. S.Ducasse 8 The problem Technically you can pass to fnb a Set object as well. Just as a Bag, a Set object accepts messages put and count. However applying fnb to a Set object will break the function's post-condition Therefore, passing a set object where a bag was expected changes behavior of some program. According to the Liskov Substitution Principle (LSP), a Set is not substitutable for a Bag -- a Set cannot be a subtype of a Bag.
  • 9. S.Ducasse 9 Another function (define (fns set) (send set 'put 5) (send set 'count 5)) The behavior of this function is: given a Set, the function adds an element into it and returns 1.
  • 10. S.Ducasse 10 Problem two If you pass to this function a bag (which -- just as a set -- replies to messages put and count), the function fns may return a number greater than 1. This will break fns's contract, which promised always to return 1.
  • 11. S.Ducasse 11 Finally Therefore, from the OO point of view, neither a Bag nor a Set are a subtype of the other. Bag and Set only appear similar. The interface or an implementation of a Bag and a Set appear to invite subclassing of a Set from a Bag (or vice versa). Doing so however will violate the LSP -- and you have to brace for very subtle errors. Sets and Bags are very simple types, far simpler than the ones you deal with in a production code. either. It's manual work -- you have to see the problem
  • 12. S.Ducasse 12 Watch out Alas, LSP when considered from an OOP point of view is undecidable. You cannot count on a compiler for help in pointing out an error. You cannot rely on regression tests
  • 13. S.Ducasse 13 In C++ typedef int const * CollIterator; // Primitive but will do class CBag { public: int size(void) const; // The number of elements in the bag virtual void put(const int elem); // Put an element into the bag int count(const int elem) const; // Count the number of occurrences // of a particular element in the bag virtual bool del(const int elem); // Remove an element from the bag // Return false if the element // didn't exist CollIterator begin(void) const; // Standard enumerator interface CollIterator end(void) const; CBag(void); virtual CBag * clone(void) const; // Make a copy of the bag private: // implementation details elided };
  • 14. S.Ducasse 14 CSet class CSet : public CBag { public: bool memberof(const int elem) const { return count(elem) > 0; } // Overriding of CBag::put void put(const int elem) { if(!memberof(elem)) CBag::put(elem); } CSet * clone(void) const { CSet * new_set = new CSet(); *new_set += *this; return new_set; } CSet(void) {} };