liskov-substitution-principle

SOLID Design Principles : Liskov Substitution Principle and Dependency Inversion Principle

笑着哭i 提交于 2021-02-18 17:26:30
问题 Just a thought and a question to the Stack Overflow and Microsoft Development Community about the OO software design principles called SOLID. What is the difference between the Liskov Substitution Principle and the Dependency Inversion Principle please ? I have thought about this for a while and I'm not sure of the difference. Please could you let me know ? Any thoughts / feedback very welcome. 回答1: Liskov's substitution Principle states the following: A class should be directly substitutable

Why doesn't Rust support trait object upcasting?

空扰寡人 提交于 2021-01-28 05:36:17
问题 Given this code: trait Base { fn a(&self); fn b(&self); fn c(&self); fn d(&self); } trait Derived : Base { fn e(&self); fn f(&self); fn g(&self); } struct S; impl Derived for S { fn e(&self) {} fn f(&self) {} fn g(&self) {} } impl Base for S { fn a(&self) {} fn b(&self) {} fn c(&self) {} fn d(&self) {} } Unfortunately, I cannot cast &Derived to &Base : fn example(v: &Derived) { v as &Base; } error[E0605]: non-primitive cast: `&Derived` as `&Base` --> src/main.rs:30:5 | 30 | v as &Base; | ^^^^

Liskov substitution principle violation

主宰稳场 提交于 2021-01-28 00:57:32
问题 From Wikipedia, Liskov's notion of a behavioral subtype defines a notion of substitutability for objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g. correctness). Suppose the following class hierarchy: The base abstract class - AnimalWithFur . It has a read-only property furColor that is overridden in successors. Base class's successor - Cat , which overrides

How to think about polymorphism with subtyping

我们两清 提交于 2021-01-27 12:09:40
问题 The Liskov Substitution Principle states: Invariants of the supertype must be preserved in a subtype. I'm particularly interested with the intersection of this principle and polymorphism. In particular subtype-polymorphism though, in fact, this seems to be the case with parametric polymorphism and Haskell type classes. So, I know that functions are subtypes when their arguments are contravariant and their return types covariant. We can assume that methods are just functions with an implicit

How to think about polymorphism with subtyping

╄→尐↘猪︶ㄣ 提交于 2021-01-27 12:05:50
问题 The Liskov Substitution Principle states: Invariants of the supertype must be preserved in a subtype. I'm particularly interested with the intersection of this principle and polymorphism. In particular subtype-polymorphism though, in fact, this seems to be the case with parametric polymorphism and Haskell type classes. So, I know that functions are subtypes when their arguments are contravariant and their return types covariant. We can assume that methods are just functions with an implicit

How to define stricter typing in child class inherited methods in PHP?

落爺英雄遲暮 提交于 2020-12-26 08:10:21
问题 Assume I have three classes like so. abstract class A { abstract protected function doSomething($object): array; } class B extends A { protected function doSomething(SomeObject $object): array { // something } } class C extends A { protected function doSomething(OtherObject $object): array { // something } } According to principles of inheritance as PHP does it, the structure described above is not legal PHP code, as the method definitions are not compatible with the base class. I can, of

Does Liskov Substitution Principle also apply to classes implementing an interface?

戏子无情 提交于 2020-02-03 11:00:22
问题 LSP states that classes should be substitutable for their base classes, meaning that derived and base classes should be semantically equivalent. But does LSP also apply to classes implementing an interface? In other words, if an interface method implemented by a class is semantically different from what user expects it to be, would this be considered as a violation of LSP? Thank you 回答1: No It only applies to subtypes. See the Wikipedia article for a brief summary. If you have a class B that

Hierarchy violates Liskov - so what?

给你一囗甜甜゛ 提交于 2020-01-24 03:16:05
问题 I am using an API that violates the Liskov substitution principle : it throws its own Exception type that extends Exception, but puts the exception message from the base class in a new ErrorCode field and puts its own (useless) message in the Message field. Therefore to display the correct message I need to cast the Exception to the DerivedException type and use the ErrorCode field. If I treat it as an Exception object I get the wrong message. Now this irks me on a stylistic level, but it is

When “if else”/“instance of” are inevitable, how do we improve the design apart from using visitor pattern?

偶尔善良 提交于 2020-01-23 07:07:52
问题 When we have an object hierarchy that is purely a inheritance of semantic and not of behaviors,then inevitably we need to write "instanceof" or "if/else" everywhere to do run time type checking. E.g. If I have a object hierarchy which has Class Function Class Average extends Function Class Sum extends Function Class Max extends Function If there is a method called calculate() in these classes, then we do not have problem, we can just take the advantage of polymorphism and this design

Is this correct understanding of Liskov Substitution Principle

混江龙づ霸主 提交于 2020-01-13 06:48:07
问题 This was asked to me in an interview. I answered him by saying that for same set of input both parent and child should produce same set of output. If child wants to extend parent's functionality it should do only on new inputs outside the range supported by parent. In that way, child will maintain the contract made by it's parent. I gave him example, that an api may be using a parent like this if(parent.getOutput(10) == 5){/*do something */} If child produced a different output here then that