liskov-substitution-principle

Does GWT's ActivityMapper violate the Liskov Substitution Principle?

会有一股神秘感。 提交于 2019-12-08 03:25:46
问题 In my GWT application, I have a class like so: public class AppActivityMapper implements ActivityMapper { @Override public Activity getActivity(Place place) { if(place instanceof ThisPlace) { return new ThisActivity((ThisPlace)place); } if(place instanceof ThatPlace) { return new ThatActivity((ThatPlace)place); } if(place instanceof AnotherPlace) { return new AnotherActivity((AnotherPlace)place); } // you get the idea } } The ActivityMapper, Activity, and Place objects are part of the

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

别说谁变了你拦得住时间么 提交于 2019-12-07 03:54:26
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 ! 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 value. Suppose we have this: let weather = WeatherType.cloudy(coverage:1) Then this is perfectly legal: switch

Overriding getPreferredSize() breaks LSP

前提是你 提交于 2019-12-06 20:28:56
问题 I always see advices in this site of overriding getPreferredSize() instead of using setPreferredSize() as shown in these previous threads for example. Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Overriding setPreferredSize() and getPreferredSize() See this example: public class MyPanel extends JPanel{ private final Dimension dim = new Dimension(500,500);

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

孤人 提交于 2019-12-06 18:22:01
问题 Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection<T> violates Liskov. ICollection<T> 's contract exposes Add and Remove operations, but the read only subtype does not satisfy this contract. For example, IList<object> collection = new List<object>(); collection = new System.Collections.ObjectModel.ReadOnlyCollection<object>(collection); collection.Add(new object()); -- not supported

C# Interface Implementation relationship is just “Can-Do” Relationship?

怎甘沉沦 提交于 2019-12-05 18:22:33
问题 Today somebody told me that interface implementation in C# is just "Can-Do" relationship, not "Is-A" relationship. This conflicts with my long-time believing in LSP(Liskov Substitution Principle). I always think that all inheritance should means "Is-A" relationship. So, If interface implementation is just a "Can-Do" relationship. What if there is a interface "IHuman" and "IEngineer", and one class "Programmer" inherits from "IHuman" & "IEngineer"? Surely, a "Programmer" Is A "IHuman" and A

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

有些话、适合烂在心里 提交于 2019-12-05 08:27:14
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 satisfies the LSP. However what if we do not want to add this calculate() method to this hierarchy for some

Overriding getPreferredSize() breaks LSP

微笑、不失礼 提交于 2019-12-05 01:37:43
I always see advices in this site of overriding getPreferredSize() instead of using setPreferredSize() as shown in these previous threads for example. Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Overriding setPreferredSize() and getPreferredSize() See this example: public class MyPanel extends JPanel{ private final Dimension dim = new Dimension(500,500); @Override public Dimension getPreferredSize(){ return new Dimension(dim); } public static void main

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

試著忘記壹切 提交于 2019-12-04 22:24:53
Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection<T> violates Liskov. ICollection<T> 's contract exposes Add and Remove operations, but the read only subtype does not satisfy this contract. For example, IList<object> collection = new List<object>(); collection = new System.Collections.ObjectModel.ReadOnlyCollection<object>(collection); collection.Add(new object()); -- not supported exception There is clearly a need for immutable collections. Is there something broken about .NET's way of

Can I implement a series of reusable tests to test an interface's implementation?

你说的曾经没有我的故事 提交于 2019-12-04 21:12:56
问题 I am writing a series of collection classes in C#, each of which implement similar custom interfaces. Is it possible to write a single collection of unit tests for an interface, and automatically run them all on several different implementations? I would like to avoid any duplicated testing code for each implementation. I'm willing to look into any framework (NUnit, etc.) or Visual Studio extension to accomplish this. For those looking to do the same, I posted my concrete solution, based off

When adhering to Liskov Substitution Principle (LSP) can a child class implement additional interface?

余生长醉 提交于 2019-12-04 15:49:53
问题 Consider this ruby example class Animal def walk # In our universe all animals walk, even whales puts "walking" end def run # Implementing to conform to LSP, even though only some animals run raise NotImplementedError end end class Cat < Animal def run # Dogs run differently, and Whales, can't run at all puts "running like a cat" end def sneer_majesticly # Only cats can do this. puts "meh" end end Does method sneer_majesticly violate LSP, being defined only on Cat, since this interfaces is