Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [liskov-substitution]

For questions about Liskov substitution principle in object-oriented design.

0
votes
1answer
101 views

Decorator or Facade

I face an issue if to use a decorator or facade pattern to accomplish my needs. Imagine a client wants to play a video. He can use the interface public interface IVideoPlayer { // Prepares ...
0
votes
1answer
497 views

Why does the Square–Rectangle problem violate SOLID principles?

My previous question was just with class diagram and because of some comments I thought I try to find an example with a little piece of code for the diagram to see if principles correct understood? ...
1
vote
2answers
226 views

Liskov Substitution Principle

I am trying to wrap my mind around a Liskov Substitution Principle and I came across this article. SOLID Class Design: The Liskov Substitution Principle In this article, the author gives an example ...
4
votes
4answers
315 views

Liskov Substitution Principle in Clean Architecture

Sorry for the unspecific title but I can't find a concise way to state my issue. I am reading the book Clean Architecture, by Robert C. Martin. It has a short chapter about the LSP with a couple ...
3
votes
5answers
232 views

Does deciding about Liskov sub.princple compliance require a contract?

I have read two opinions on the subject. Let's assume the following simple code: class Enemy { public virtual void CheckHealth() { if (Life <= 0) Dispose(); } } ...
1
vote
1answer
234 views

Why was WCF designed to violate Liskov Substitution Principle?

When you want to send your own types over WCF, you have to add lots of [ServiceKnownType(typeof(MyFirstConcreteType))] to the interface definition of your contract. You cannot use [ServiceKnownType(...
0
votes
0answers
44 views

ASP.NET RoleProvider AddUsersToRoles - Removing exception if user already has role

I'm implementing a custom RoleProvider in my ASP.NET web app, however I had a question about the example implementations of AddUsersToRoles and it's Remove counterpart shown here: https://msdn....
1
vote
2answers
213 views

Does it violate SRP to reject subclass method parameter

I was having a good read on Eric Lippert's blog about Wizard and Warriors. It suggests the creation of a Rules class, quote: We keep on talking about “rules”, and so apparently the business ...
7
votes
2answers
656 views

Liskov's substitution principle : If subtype has some extra behaviour implemented, which is not present in type, then is this violation of LSP?

In my quest to write better, cleaner code, I am learning about SOLID principles. In this, LSP is proving to be little difficult to grasp properly. My doubt is what if I have some extra methods in my ...
4
votes
1answer
97 views

Converting Interfaces to Sub types. Is this bad SE?

In the company I work for, I keep seeing IManager interfaces being converted into their real types and lots of "instanceof / TypeOf" if statement checks. For example: IManager manager // passed to ...
4
votes
2answers
352 views

Argument contravariance, real world purpose and usage?

I have read a few questions on the SO and elsewhere and still do not understand well where this "widening" of a parameter type can be helpful, i.e. compliying to Liskov substitution principle. The ...
0
votes
2answers
243 views

Achieving Liskov-compliant contravariance in method arguments in C#

Trying to practice LSP, the following is not really clear to me: Liskov requirements (some) -There must be contravariance of the method arguments in the subtype. – There must be covariance ...
3
votes
2answers
314 views

Liskov principle: subclasses can have stronger invariants. How could it work?

Learning about the Liskov principle, I do understand that preconditions can be weakened and postconditions can be stronger in subtypes. However, I do not understand how invariants can be stronger in ...
3
votes
3answers
170 views

Liskov substitution for voids and weakened preconditions

I am learning a lot about this principle (also thanks to two answers I received here) and would like to elaborate on another point that somebody mentioned. 1) Is the following a violation of LSP? ...
7
votes
6answers
3k views

Liskov Substitution principle - strengthening preconditions

I am a bit confused as for what it really means. In the related questions (Is this a violation of the Liskov Substitution Principle?), it was said that the example clearly violates LSP. But I wonder, ...
42
votes
3answers
6k views

What's the difference between a subclass and a subtype?

The highest rated answer to this question about the Liskov Substitution Principle takes pains to distinguish between the terms subtype and subclass. It also makes the point that some languages ...
13
votes
2answers
2k views

Stack extending LinkedList. A violation of Liskov Substitution Principle?

A class LinkedList exists with functions such as add_first(), add_last(), add_after(), remove_first(), remove_last() and remove() Now there is a class Stack that provides functionalities such as push(...
1
vote
3answers
607 views

Is the Liskov Substitution Principle concerned with protecting state invariants?

Wiki says: Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (...
4
votes
3answers
297 views

How to loosen input contracts by inheritance?

According to LSP wiki: Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with ...
5
votes
5answers
431 views

Liskov, Rectangles, Squares, and Null Objects

I keep thinking I have my head wrapped around the Liskov Substitution Principle, and then I realize I don't. Here's where I've gathered from StackExchange: Subclassing Square from Rectangle violates ...
2
votes
1answer
438 views

Pattern for subclass overload with different arguments

I'm in the process of writing an bidirectional, asynchronous socket server and server handler. The base handler class I'm working off of is as follows: class BaseAsyncSocketHandler: async def ...
2
votes
2answers
264 views

Confusion about strengthening/weakening preconditions/postconditions

I've recently become incredibly confused about the notion of "strengthening/weakening a precondition/postcondition". I think my confusion lies in my interpretation of the words "precondition" and "...
11
votes
4answers
2k views

How to specify a precondition (LSP) in an interface in C#?

Let's say that we have the following interface - interface IDatabase { string ConnectionString{get;set;} void ExecuteNoQuery(string sql); void ExecuteNoQuery(string[] sql); //Various ...
0
votes
2answers
103 views

Liskov's substitution principle and prototypical languages

Do prototypical languages provide a remedy from Liskov's problem? So the way I see this is: a subclass is very tightly coupled with it's superclass and this creates subtle side effects when ...
10
votes
7answers
3k views

Should rectangle inherit from square? [duplicate]

So, we're all probably familiar with the example provided in most textbooks of the Liskov substitution problem involving a square inheriting from rectangle. The objection to this approach is that ...
14
votes
2answers
904 views

Real World - Liskov Substitution Principle

Background: I am developing a messaging framework. This framework will allow: sending of messages over a service bus subscribing to queues on the message bus subscribing to topics on a message bus ...
0
votes
2answers
160 views

How to manage context objects in a multi tenant application?

I'm building a Web API that interfaces SharePoint (an external web service). To get at connection to the external service I need to create instances of ClientContext with some kind of token. For this ...
7
votes
1answer
388 views

Can it be OK to derive from a standard library type while also violating the Liskov principle?

I would like to define a shared pointer whose operator== compares the pointed to values instead of the pointers, something like this: template <typename T> struct deref_shared_ptr: std::...
1
vote
1answer
164 views

Are new empty Java marker interfaces created to satisfy an existing method contract a code smell?

Suppose I have a POJO A and some client code that serves it. I have created POJO B, which doesn't have anything in common with A, but I want to use it in the methods that use A, so I decided to create ...
8
votes
2answers
283 views

How to resolve LSP violation based on minimum precondition

I have a class Party that has a constructor that takes a Collection<Foo>. I plan to have two subclasses NpcParty and PlayerParty. All Party instances have an upper limit on the size of the input ...
8
votes
4answers
263 views

When in your language classes are objects too, does the Liskov substitution principle apply to their interfaces?

According to Wikipedia the Liskov substitution principle states that objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program ...
11
votes
6answers
2k views

Can the circle-ellipse problem be solved by reversing the relationship?

Having Circle extend Ellipse breaks the Liskov Substition Principle, because it modifies a postcondition: namely, you can set X and Y independently to draw an ellipse, but X must always equal Y for ...
6
votes
3answers
1k views

Does the visitor pattern violate the Liskov Substitution Principle

When using the Visitor pattern, I have observed the following: The visitor has weak dependencies to concrete types (each visit method has the concrete element as a parameter or is a method that ...
4
votes
2answers
464 views

Do common MVC frameworks violate the LSP and is there a MVC framework which does not?

You who have worked with a framework implementing the MVC architectural pattern most likely know how these frameworks are usually implemented. They contain a base Controller class, which you extend, ...
20
votes
5answers
2k views

Do special-cases with fallbacks violate the Liskov Substitution Principle?

Let's say I have an interface FooInterface that has the following signature: interface FooInterface { public function doSomething(SomethingInterface something); } And a concrete class ...
6
votes
6answers
3k views

Does this code solve the square/rectangle Liskov Substution Principle example?

I just wanted to check that I understand the LSP correctly and can solve it. I am taking the classic rectangle/square problem and attempting a solution: class Rectangle{ public $width; public ...
1
vote
2answers
277 views

Liskov principle with different constructor parameters

My first question is, is the Liskov principle applied even on constructor declaration? I mean there, am I forced to have exactly the same number / types of parameter in my constructor for each (...
3
votes
1answer
144 views

How do you keep up with 'requiring new methods in an interface' (Following LSP and adding new methods to interface seems to violate ISP)

I have a game that deals with opening and closing doors and the Door Engine deals with IDoor interface which has Open() and Close() contracts So far so good. the game is tested and works fine. Now a ...
1
vote
1answer
382 views

Where does the Liskov Substitution Principle generally lie in different constructor parameter lists?

There are two other questions I've posted that dealt with specific cases of this: Where does the Liskov Substitution Principle lie in a subclass passing extra arguments to similar, tightly-related ...
1
vote
1answer
204 views

Where does the Liskov Substitution Principle lie in a subclass passing extra arguments to similar, tightly-related callbacks?

Take the following examples, which have been abbreviated: BasicButton: public class BasicButton { private var m_fOnClick:Function; public function BasicButton(pOnClick:Function) { ...
2
votes
2answers
142 views

Do the implementations of the Node interface in Web API violate Liskov Substitution Principle?

MDN article on Node interface states that interfaces [that inherit from Node interface] may return null in particular cases where the methods and properties are not relevant. They may throw an ...
0
votes
5answers
700 views

Liskov Substitution and SRP Principle violation - how best to structure this scenario?

While learning SRP and LSP, I'm trying to improve the design of my code to comply best with both of these principles. I have an employee class that has a calculatePay method on it. Firstly, I believe ...
0
votes
1answer
119 views

Matching the superclass's constructor's parameter list, is treating a null default value as a non-null value within a constructor a violation of LSP? [duplicate]

I kind of ran into this when messing around with FlashPunk, and I'm going to use it as an example. Essentially the main sprite class is pretty much class Entity. Entity's constructor has four ...
12
votes
5answers
4k views

Is method overriding always a violation of Liskov Substitution Principle? [duplicate]

Overriding a method originally defined in the super class, by definition means this method will do different things when invoked on an object of the base class or an object of the subclass. So does ...
4
votes
1answer
948 views

Liskov substitution principle with abstract parent class

Does Liskov substitution principle apply to inheritance hierarchies where the parent is an abstract class the same way if the parent is a concrete class? The Wikipedia page list several conditions ...
4
votes
3answers
1k views

Does this Decorator implementation violate the Liskov Substitution Principle?

Please consider the following implementation of the Decorator design pattern: WordBank objects store strings and return them to the client through the method getWords(). The decorator class, ...
2
votes
1answer
2k views

Liskov principle: violation by type-hinting

According to the Liskov principle, a construction like the one below is invalid, as it strengthens a pre-condition. I know the example is pointless/nonsense, but when I last asked a question like ...
7
votes
4answers
2k views

Is throwing an error in unpredictable subclass-specific circumstances a violation of LSP?

Let's say I wanted to create a Java List<String> (see spec) implementation that uses a complex subsystem, such as a database or file system, for its store so that it acts as a persistent ...
4
votes
2answers
2k views

Confusion with Factory pattern regarding Liskov's Substitution Principle, code maintainability and Unit Testing?

I have a confusion regarding Factory Pattern there are basically two ways You can implement that. Approach 1: public interface IProductFactory { IProduct GetProductA(); IProduct GetProductB()...
18
votes
3answers
2k views

Is there a specific name for the “Square inherits from Rectangle” paradox?

A certain failure of OOP is shown with a class Square inheriting from Rectangle, where logically Square is a specialization of Rectangle and should therefore inherit from it, but everything falls ...