Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm facilitating a session next week on the Liskov Substitution Principle and I was wondering if anyone had any examples of violations 'from the trenches'? I'm looking for something other than uncle Bob's rectangle - square problem and the persistent set problem he talks about in A-PPP (although that is a great example).

So far I'm using the example of a (very simple) List and an IndexedList as the 'correct' use of inheritance. And the addition of a Set to this hierarchy as a violation (as a Set is distinct; strengthening the pre condition of the Add method).

I've also taken this great example and it's solution from this question

Both those examples are great but I'm looking for something more subtle and harder to spot. So far I've come up with nothing so if you've got a great, subtle example post it up. Also, any metaphors you've come across that helped you understand LSP would be really useful too.

share|improve this question

closed as not constructive by Thomas Owens Nov 21 '12 at 11:27

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
1  
This seems very much like polling for examples, which isn't suitable for our question and answer format. The linked question has an excellent answer that will probably be of value. –  Thomas Owens Nov 21 '12 at 11:27
    
which is the exact example I didn't want to use. Thanks to the others though, especially jgauffin. –  james lewis Nov 21 '12 at 11:34
add comment

2 Answers

up vote 2 down vote accepted

The MembershipProvider of ASP.NET contains several examples of LSP violations.

For instance:

  • The base class say that you can supply your own key (object providerUserKey) to create a relation between the provider tables and your own. However, all providers will only accept a Guid as a provider key. Even Microsofts own providers. Sample: http://msdn.microsoft.com/en-us/library/6tc47t75(v=vs.100).aspx

  • Many providers throw NotSupportException etc for one or more methods.

share|improve this answer
add comment

in one of my other answers I described how java's UndoManager violates it by relaxing the postcondition of the undo and redo from its superclass CompoundEdit (and the spirit of undo and redo in the interface UndoableEdit)

I ended up rolling my own UndoBuffer (yeah I changed the name) which does what UndoManager should do

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.