SOLID-Principles | Code samples and UML class diagrams | Architecture library
kandi X-RAY | SOLID-Principles Summary
kandi X-RAY | SOLID-Principles Summary
Code samples and UML class diagrams of Anti-SOLID and SOLID Design Patterns
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