SOLID-principles | Code examples of the SOLID principles
kandi X-RAY | SOLID-principles Summary
kandi X-RAY | SOLID-principles Summary
Code examples of the SOLID principles. A class should take care of a Single Responsibility. This Add method does too much, it shouldnt know how to write to the log and add a customer. Good Way, not violating the single responsibility principle. Now we abstract the logger, so its writing the error. Prefer extension over modification. Violating the Open Closed Principle This is bad, because at the moment, there are 2 types of customer, if we want to add another customer type we have to add a if else below. So Modifying the existing code. This is better, because we structure the code so its easier to extend and hard to modify. Parent class should be able to refer child objects seamlessly during runtime polymorphism. BAD: Violating Liskov substitution principle The parent should easily the replace the child object and not break any functionality, only lose some. e.g. here, we don't want this to add an enquiry so we have to throw a new exception, that is violating the principle. Client should not be forced to use an interface, if it doesn't need it. If we want to add more functionality, don't add to existing interfaces, segregate them out. Just create another interface, that a class can ALSO extend from. High level modules should not depend on low-level modules, but should depend on abstraction. Bad: We are relying on the customer to say that we are using a File Logger, rather than another type of logger, e.g. EmailLogger. Good: We pass in a Logger interface to the customer so it doesnt know what type of logger it is.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SOLID-principles
SOLID-principles Key Features
SOLID-principles Examples and Code Snippets
Community Discussions
Trending Discussions on SOLID-principles
QUESTION
I'm trying to create an generic service-interface per logic-class to communicate with the database. See code examples for an explanation better than words.
For example:
Foo
...ANSWER
Answered 2020-Jul-05 at 13:39Keep it simple! Create FooService that implements IFooService. FooLogic should be removed. You can implement the logic inside the method CreateFoo.
Since FooService will impement all the methods, you can call ReadBar() instead of _barService.ReadBar(), there is no need for composition since you already have IFooService inheriting from all other interfaces.
This way, we are still respecting the dependency injection pattern.
QUESTION
Currently I am working on the Racing-Car-Katas. The goal is to refactor a peach of code so that it follows the solid-principles.
I try to add the Dependency Inversion Principle. Where I can pass a dependency through the constructor.
Initial SituationInsid the class Alarm
is the dependency Sensor
which generates a psiPressureValue
.
ANSWER
Answered 2018-Oct-06 at 00:11Ok you want to break the direct coupling between Alarm and Sensor.
Your proposed solutions shows two constructors, one injecting the Sensor object (created externally) and one creating Sensor object directly. You should drop:
QUESTION
I'm looking over the SOLID principles and came across some bits of code for SRP.
I have this code, but I do not have a clue why I'd have an interface, declared in the way that I do below? What good does this do to me? I found here, it shows a solution, but it doesn't really explain how the code works better or why things are the way they are.
The thing I don't understand is:
IGateUtility _gateUtility;
In public class ServiceStation
, and right below it is the constructor with IGateUtility
as parameter. Why is this written like that? What's the parameter I would have to pass.
ANSWER
Answered 2018-May-23 at 20:48The constructor parameter is an example of dependency injection, specifically the technique known as "constructor injection" (which is usually the preferred technique).
The ServiceStation
isn't supposed to contain the logic of a IGateUtility
because it doesn't have anything to do with the gate (Single Responsibility Principle). It does need to use the gate however so you pass an object implementing IGateUtility
in.
Generally speaking I don't think inheritance would make sense in this instance regardless; but there is a principle that states:
Prefer Composition to Inheritance
Which basically means; inject (compose) objects to gain access to their behavior instead of inheriting from them.
QUESTION
I am learning about LSP in detail and I do understand why strenghtening preconditions violates the principle (using the example from http://www.ckode.dk/programming/solid-principles-part-3-liskovs-substitution-principle/#contravariance):
...ANSWER
Answered 2018-Jan-18 at 20:30If you weaken a precondition, the subtype is still compatible with places expecting the supertype. It may not throw an exception where the base class does normally, but that is okay, because throwing less exceptions should not break the consuming code. If the calling code is built around the assumption that exceptions get thrown in certain places, and uses that for primary control flow of the application, the consuming code should probably be rewritten.
Also, I think your second code sample is wrong.
If the preconditions of the base class really must be enforced all the time, a better implementation would be to create a data type that encapsulates those rules, and pass that as a parameter. That way it is not in the hand of subclasses, it is part of the new class's constructor.
Ex:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SOLID-principles
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page