solid-principles

Is this precondition a violation of the Liskov Substitution Principle

二次信任 提交于 2021-02-19 01:16:13
问题 I have 3 classes, Account , CappedAccount , UserAccount , CappedAccount , and UserAccount both extend Account . Account contains the following: abstract class Account { ... /** * Attempts to add money to account. */ public void add(double amount) { balance += amount; } } CappedAccount overrides this behavior: public class CappedAccount extends Account { ... @Override public void add(double amount) { if (balance + amount > cap) { // New Precondition return; } balance += amount; } } UserAccount

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

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 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

Interface Segregation Principle and default methods in Java 8

China☆狼群 提交于 2020-08-27 21:55:13
问题 As per the Interface Segregation Principle clients should not be forced to implement the unwanted methods of an interface ...and so we should define interfaces to have logical separation. But default methods introduced in Java 8 have provided the flexibility to implement methods in Java interfaces. It seems Java 8 has provided the feasibility to enhance an interface to have some methods not related to its core logic, but with some default or empty implementation. Does it not violate the ISP?

Interface Segregation Principle and default methods in Java 8

百般思念 提交于 2020-08-27 21:54:22
问题 As per the Interface Segregation Principle clients should not be forced to implement the unwanted methods of an interface ...and so we should define interfaces to have logical separation. But default methods introduced in Java 8 have provided the flexibility to implement methods in Java interfaces. It seems Java 8 has provided the feasibility to enhance an interface to have some methods not related to its core logic, but with some default or empty implementation. Does it not violate the ISP?

In SOLID, what is the distinction between SRP and ISP? (Single Responsibility Principle and Interface Segregation Principle)

一世执手 提交于 2020-06-24 04:56:09
问题 How does the SOLID "Interface Segregation Principle" differ from "Single Responsibility Principle"? The Wikipedia entry for SOLID says that ISP splits interfaces which are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them However, to me that sounds like just applying the SRP to interfaces as well as classes. After all, if an interface is only responsible for just one conceptual thing, than you wouldn't be able

StackOverFlow in the implementation of the Dependency Inversion Principle for cross-refenced classes

做~自己de王妃 提交于 2020-06-01 07:03:40
问题 The Situation: I have three classes: Society, Father and Son. The Society has a list of Fathers. Each Father has one son. It is necessary in this application that each Father knows who his Son is and vice-versa each Son knows who his Father is. I have used direct cross referencing by assigning the Father as a property of the Son, and the Son as a property of the Father class, as below: public class Society : ISociety { public List<IFather> Fathers {get; set;} } public class Father : IFather {

Why singleton breaks open/closed principle?

北城余情 提交于 2020-05-13 03:38:41
问题 Could anyone tell me why does singleton break open/closed principle? Is it because there could be problems with inheriting from that class? 回答1: For a class to be "open" it must be possible to inherit from it. Inheritance is an "is-a" relationship. If you inherit from a singleton-class then instances of the child-class are also instances of the parent class due to the "is-a" relationship, meaning you can suddenly have multiple instances of the singleton class. If the singleton class inhibits

Why singleton breaks open/closed principle?

时光毁灭记忆、已成空白 提交于 2020-05-13 03:37:32
问题 Could anyone tell me why does singleton break open/closed principle? Is it because there could be problems with inheriting from that class? 回答1: For a class to be "open" it must be possible to inherit from it. Inheritance is an "is-a" relationship. If you inherit from a singleton-class then instances of the child-class are also instances of the parent class due to the "is-a" relationship, meaning you can suddenly have multiple instances of the singleton class. If the singleton class inhibits