liskov-substitution-principle

How can I avoid breaking Liskov Substitution Principle (LSP)?

时光毁灭记忆、已成空白 提交于 2019-11-29 00:40:00
问题 I am in a situation very similar to what Steve McConnell's in Code Complete has mentioned . Only that my problem is based of Vehicles and Trike happens to be on that by law falls in the category of Cars . Cars had four wheels until now . Any way my domain is unnecessarily complex so it is easy to stick with cats example below. Be suspicious of classes that override a routine and do nothing inside the derived routine This typically indicates an error in the design of the base class. For

Liskov Substition and Composition

风流意气都作罢 提交于 2019-11-28 20:52:30
Let say I have a class like this: public sealed class Foo { public void Bar { // Do Bar Stuff } } And I want to extend it to add something beyond what an extension method could do....My only option is composition: public class SuperFoo { private Foo _internalFoo; public SuperFoo() { _internalFoo = new Foo(); } public void Bar() { _internalFoo.Bar(); } public void Baz() { // Do Baz Stuff } } While this works, it is a lot of work...however I still run into a problem: public void AcceptsAFoo(Foo a) I can pass in a Foo here, but not a super Foo, because C# has no idea that SuperFoo truly does

Any good examples of inheriting from a concrete class? [closed]

我怕爱的太早我们不能终老 提交于 2019-11-28 16:47:15
Background: As a Java programmer, I extensively inherit (rather: implement) from interfaces, and sometimes I design abstract base classes. However, I have never really felt the need to subclass a concrete (non-abstract) class (in the cases where I did it, it later turned out that another solution, such as delegation would have been better). So now I'm beginning to feel that there is almost no situation where inheriting from a concrete class is appropriate. For one thing, the Liskov substitution principle (LSP) seems almost impossible to satisfy for non-trivial classes; also many other

Should constructors comply with the Liskov Substitution Principle? [closed]

北城余情 提交于 2019-11-28 06:08:14
I usually try to make sure my object instances comply with the Liskov Substitution Principle , but I've always wondered is do people think LSP should apply to constructors too? I've tried googling for this but I haven't been able to find any strong opinions either way. I should note that most of my coding is in Ruby, but I sometimes find that my subclass constructors are slightly different from the parent class. They take the same base set of arguments, and often extra args. Sometimes this also happens with other class methods. In the back of my head this has always felt like an LSP violation,

C# return type covariance and Liskov substitution principle

你说的曾经没有我的故事 提交于 2019-11-28 03:56:57
问题 I'm trying to understand Covariance and LSP. From this question I can see C# does not support return type covariance. However Liskov substitution principle imposes covariance on return type. Does it mean it is impossible to apply this principle in C# ? Or did I missunderstand something ? 回答1: C# can still apply the Liskov substitution principle. Consider: public class Base1 { } public class Derived1 : Base1 { } public class Base2 { public virtual Base1 Method() { return new Base1(); } }

Why it's impossible to override `var` with `def` in Scala?

北战南征 提交于 2019-11-28 02:01:47
While I understand why a var cannot override a val in subclass and vice versa, I am unable to understand why does Scala not allow a def in subclass to override a var in superclass class Car { var age = 32 } class SedanCar extends Car { override def age = 54 } As var is mutable why not allow a def to override it? Can anyone please help me in understanding this? dk14 That's related to the Liskov Substitution Principle : you can't assign weaker access privileges in subclass (even for Java) . Making var a def makes the setter def x_= (y: T ): Unit private (as @Staix said). So in case when Seadan

Any good examples of inheriting from a concrete class? [closed]

强颜欢笑 提交于 2019-11-27 19:57:12
问题 Background: As a Java programmer, I extensively inherit (rather: implement) from interfaces, and sometimes I design abstract base classes. However, I have never really felt the need to subclass a concrete (non-abstract) class (in the cases where I did it, it later turned out that another solution, such as delegation would have been better). So now I'm beginning to feel that there is almost no situation where inheriting from a concrete class is appropriate. For one thing, the Liskov

Can anyone provide an example of the Liskov Substitution Principle (LSP) using Vehicles?

こ雲淡風輕ζ 提交于 2019-11-27 17:31:49
The Liskov Substitution Principle states that a subtype should be substitutable for that type (without altering the correctness of the program). Can someone please provide an example of this principle in the domain of vehicles (automotives)? Can someone please provide an example of a violation of this principle in the domain of vehicles? I've read about the square/rectangle example, but I think that an example with vehicles will give me a better understanding of the concept. For me, this 1996 Quote from Uncle Bob ( Robert C Martin ) summarises the LSP best: Functions that use pointers or

Liskov substitution principle - no overriding/virtual methods?

£可爱£侵袭症+ 提交于 2019-11-27 09:23:25
问题 My understanding of the Liskov substitution principle is that some property of the base class that is true or some implemented behaviour of the base class, should be true for the derived class as well. I guess this would mean when a method is defined in a base class, it should never be overrided in the derived class - since then substituting the base class instead of the derived class would give different results. I guess this would also mean, having (non-pure) virtual methods is a bad thing?

Should constructors comply with the Liskov Substitution Principle? [closed]

亡梦爱人 提交于 2019-11-27 01:12:18
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . I usually try to make sure my object instances comply with the Liskov Substitution Principle, but I've always wondered is do people think LSP should apply to constructors too? I've tried googling for this but I haven't been able to find any strong opinions either way. I