liskov-substitution-principle

Liskov substitution principle and Streams

空扰寡人 提交于 2019-12-19 05:48:38
问题 Does the fact that there are Stream derived classes that cannot be written or sought break the Liskov substitution principle? For example, the NetworkStream cannot be sought, it will throw a NotSupportedException if the method Seek is called. Or because the existence of CanSeek flag it is ok? Considering the well known example of Square inheriting from Rectangle ... would the addition of the flags DoesHeightAffectsWidth and DoesWidthAffectsHeight to Rectangle fix the issue? Doesn't this open

Using the Strategy Pattern to avoid downcasting

主宰稳场 提交于 2019-12-13 15:29:30
问题 I was reading on this site about the Liskov substitution principle. It states: As per the LSP, functions that use references to base classes must be able to use objects of the derived class without knowing it. In simple words, derived classes must be substitutable for the base class. According to this page, if you override a method in the base class and it does nothing or throws an exception, you're in violation of the principle. Suppose I had an abstract class called Weapon , and the

Does the Liskov Substitution Principle apply to subtype which inherited from abstract class?

喜你入骨 提交于 2019-12-13 12:55:41
问题 loosely speaking, Liskov Substitution Principle states that a derived class can be substitute in place of the base class without affecting the user. In the case when the base class is an abstract class, which means no user is using an instance of the base class, does the Liskov inheritance restrictions still apply to the derived class? 回答1: Just because you can't instantiate a particular class does not mean that you can't use it. In this scenario, the calling code is using the abstract base

Why use type substitution [closed]

心不动则不痛 提交于 2019-12-12 02:41:32
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Can anyone explain to me what is the need of using type substitution? e.g. class Circle extends Shape{ ... } . . . class Main{ public static void main(String[] args){ Shape s = new Circle(); ... } } What possible benifit can we get from the above code? Normally, public static void

What does Liskov Substitution Principle preserve? [duplicate]

独自空忆成欢 提交于 2019-12-11 20:10:56
问题 This question already has answers here : What is an example of the Liskov Substitution Principle? (29 answers) Closed 7 months ago . As I've read the substitution of objects of a concrete type by instances of a subclass of that concrete type must preserve a program's correctness, a program's invariants. I'd like to know what exactly is meant by correctness and invariants of a program? 回答1: Let's say you have a class Animal and someone asks you what it's supposed to do, what it can be used for

Is deriving BinaryTreeNode from GraphNode a violation of Liskov's Substitution Princple

廉价感情. 提交于 2019-12-11 17:53:35
问题 The discussion comes up here: Changing visibility of method in inherited class question is: is really "BTNode extends GraphNode" design a violation of Liskov's Substitution Princeple? As an "similar" example it was shown that case: Is deriving square from rectangle a violation of Liskov's Substitution Principle? but I cannot really see why that is similar. I am very new to design, could someone explain me why (if) that's the case? 回答1: In Is deriving square from rectangle a violation of

Liskov substitution principle, preconditions and abstract methods

眉间皱痕 提交于 2019-12-10 18:05:24
问题 Liskov substitution principle (LSP) says: Preconditions cannot be strengthened in a subtype. In C#, I could violate the whole principle as follows: public class A { public virtual void DoStuff(string text) { Contract.Requires(!string.IsNullOrEmpty(text)); } } public class B : A { public override void DoStuff(string text) { Contract.Requires(!string.IsNullOrEmpty(text) && text.Length > 10); } } But, what would happen if A.DoStuff would be an abstract method: public class A { public abstract

Type - Subtype relation. Something seems unclear

二次信任 提交于 2019-12-10 13:09:47
问题 I'm reading some slides of a class on object oriented programming languages and stepped into the type-subtype definition: Barbara Liskov, “Data Abstraction and Hierarchy,” SIGPLAN Notices, 23,5 (May, 1988): What is wanted here is something like the following substitution property: If for each object o_s of type S there is an object o_T of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o_S is substituted for o_T then S is a subtype of T Then it

What is a Refused Bequest?

孤街浪徒 提交于 2019-12-10 01:36:41
问题 Could someone please explain what does Refused Bequest means? I tried reading some articles and says its a kind of code smell or in wiki it tells that it is a class that overrides a method of a base class in such a way that the contract of the base class is not honored by the derived class. But in a nutshell or in a more simple terms, what is it actually? 回答1: I think you get it. Refused Bequest is a code smell. But, what type of code smell? Quoting Martin Fowler's book Refactoring: improving

Does using enum with associated value in Swift violate Liskov Substitution Principle?

痴心易碎 提交于 2019-12-08 05:14:13
问题 enum WeatherType { case cloudy(coverage: Int) case sunny case windy } I just saw this in a Swift tutorial and I can't believe they allow you to do that. Now, whenever I switch on that enum, I gotta create a special case for cloudy ! 回答1: You don't "gotta" do anything. If you don't care what the coverage is, don't ask what the coverage is. If you don't care if it's cloudy, don't ask if it's cloudy. There is nothing special about the way you write a switch for a case that has an associated