liskov-substitution-principle

Liskov Substition and Composition

落花浮王杯 提交于 2020-01-09 18:40:35
问题 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

Liskov Substition and Composition

ぐ巨炮叔叔 提交于 2020-01-09 18:38:40
问题 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

Liskov Substition and Composition

妖精的绣舞 提交于 2020-01-09 18:38:13
问题 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

Liskov Substition and Composition

若如初见. 提交于 2020-01-09 18:38:02
问题 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

Liskov substitution principle and Interface

三世轮回 提交于 2020-01-04 07:00:09
问题 Does the ICollection<T>.Add() -implementation of arrays break the Liskov substitution principle? The method results in a NotSupportedException , which does break the LSP, IMHO. string[] data = new string[] {"a"}; ICollection<string> dataCollection = data; dataCollection.Add("b"); This results in Unhandled exception: System.NotSupportedException: Collection was of a fixed size. I found a pretty similar question concerning Stream -implementations. I open a separate question, because this case

Do Collections.unmodifiableXXX methods violate LSP? [closed]

夙愿已清 提交于 2019-12-28 05:37:07
问题 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 last year . Liskov Substitution principle is one of the principles of SOLID. I have read this principle some number of times now and have tried to understand it. Here is what I make out of it, This principle is related to strong behavioral contract among the hierarchy of classes. The

Why are LSP violations in PHP sometimes fatal, and sometimes warnings?

自闭症网瘾萝莉.ら 提交于 2019-12-23 08:50:22
问题 This LSP violation raises a Fatal Error: abstract class AbstractService { } abstract class AbstractFactory { abstract function make(AbstractService $s); } class ConcreteService extends AbstractService { } class ConcreteFactory extends AbstractFactory { function make(ConcreteService $s) {} } This LSP violation also raises a Fatal Error: interface AbstractService { } interface AbstractFactory { function make(AbstractService $s); } class ConcreteService implements AbstractService { } class

Why Liskov Substitution Principle needs the argument to be contravariant?

社会主义新天地 提交于 2019-12-21 11:26:56
问题 One of the rules that Liskov Substitution Principle imposes on method signature in derived class is: Contravariance of method arguments in the subtype. If I understood correctly, it is saying that the derived class's overridding function should allow contravariant arguments(Supertype arguments). But, I could'nt understand the reason behind this rule. Since LSP talks mostly about dynamically binding the types with there subtypes(rather than supertypes) in order to achieve abstraction, so

Why Liskov Substitution Principle needs the argument to be contravariant?

江枫思渺然 提交于 2019-12-21 11:26:14
问题 One of the rules that Liskov Substitution Principle imposes on method signature in derived class is: Contravariance of method arguments in the subtype. If I understood correctly, it is saying that the derived class's overridding function should allow contravariant arguments(Supertype arguments). But, I could'nt understand the reason behind this rule. Since LSP talks mostly about dynamically binding the types with there subtypes(rather than supertypes) in order to achieve abstraction, so

Does this violate the Liskov substitution principle, and if so, what do I do about it?

被刻印的时光 ゝ 提交于 2019-12-21 05:23:13
问题 Use case: I'm using data templates to match a View to a ViewModel. Data templates work by inspecting the most derived type of the concrete type provided, and they don't look at what interfaces it provides, so I have to do this without interfaces. I'm simplifying the example here and leaving out NotifyPropertyChanged, etc., but in the real world, a View is going to bind to the Text property. For simplicity, imagine that a View with a TextBlock would bind to a ReadOnlyText and a View with a