Questions tagged [solid]
Mnemonics for set of design principles: Single responsibility, Open-closed, Liskov substitution, Interface segregation, Dependency inversion
399
questions
5
votes
7
answers
429
views
Exceptions and the Liskov Substitution Principle
Consider the following scenario.
I have an interface IService:
public interface IService
{
void DoSomething();
}
with an implementation:
public class Implementation : IService
{
// This might ...
1
vote
3
answers
225
views
In "Liskov Substitution Principle", is "invariants can't be weakened in a subtype" a kind of "postconditions can't be weakened in a subtype"?
According to Invariant rule in Liskov Substitution Principle, I know one of the form of violation of "Liskov Substitution Principle" is violating "invariants can't be weakened in a ...
2
votes
2
answers
158
views
Trade-offs between applying the open-closed principle and type safety
There's lots of questions about the open-closed principle here, but none that I found satisfied this particular problem. At $job, I'm teaching my team Rust, but I believe the nature of this question ...
0
votes
2
answers
128
views
Dependency injection - passing responsibility for dependency to a Client?
I have long standing argue about dependency injection and SOLID principles with my teammate. We both want to make an Exporter, to export data into various formats.
My approach (in PHP):
class Exporter ...
3
votes
2
answers
326
views
Invariant rule in Liskov Substitution Principle
From Liskov Substitution Principle, I am still not very clear about the invariant rule. I read through many posts but I still have doubts.
My example is picked from this blog, the example is slightly ...
3
votes
4
answers
229
views
Does interface segregation principle apply to configuration data holders?
If you have a class representing your applicative config file.
Instead of injecting that config class everywhere, would it be good application of interface segregation principle to expose several ...
2
votes
2
answers
2k
views
Is this design too convoluted?
I'm trying to experiment with SOLID in order to get a better grasp of its' concepts. With that in mind, I'm making a card game project.
There's a GameUtils project that contains the following classes:...
-1
votes
1
answer
144
views
Will I lose confidence of my code working in mocking dependency injected services? [duplicate]
Let's say that I have a class with a service that is going to be injected at runtime:
class Thing {
private magic: IMagic; // Magically injected service
public doStuff(){
// Do a lot ...
0
votes
2
answers
199
views
Possible violation of LSP when adhering to ISP?
Recently I read about ISP and wanted to implement it into my project. While implementing my design I think I found a flaw which violates LSP but I'm not sure.
Given I have a game project, in which ...
2
votes
5
answers
427
views
What exactly is the single responsibility principle? [duplicate]
I've generally understood the SRP to roughly mean:
Each class should do one thing
Exactly what "one thing" is is up for debate.
However, I've recently seen claims that the entire SRP has ...
2
votes
3
answers
239
views
How to retain the concrete type when writing base-class-oriented code?
A scenario often arises when attempting to make some existing code reusable. I introduce an interface to represent the commonality between some new feature I'm implementing and some existing ...
1
vote
3
answers
89
views
Refactoring Controllers publishing different events with MediatR library
I have Controller that basically publishes different Event based on some input criteria, simplified version of the logic is here:
[HttpPost]
public async Task<ActionResult> Create(...
6
votes
2
answers
2k
views
How to implement many-to-many relationships without violating design principles in OOP?
I have an event class that holds details of a volunteering "Event" class. There is an "EXCO" Class that can create and edit details of said Events. There is also a "Volunteer ...
2
votes
2
answers
359
views
In "Liskov Substitution Principle", are "Preconditions can't be strengthened in a subtype" & "Postconditions can't be weakened in a subtype" the same?
According to Is this a violation of the Liskov Substitution Principle?, as I understand, the top answer currently says the code below is violating "Liskov Substitution Principle":
public ...
71
votes
8
answers
17k
views
When to *not* use SOLID principles
Coming from a math background, counterexamples are equally, if not more, helpful to me for understanding concepts than examples. I've seen many, many examples of when and how to use the SOLID ...
2
votes
1
answer
1k
views
Clean architecture - how to manage use cases that depends on external apis
How to apply clean architecture concepts in a software that depends on External APIs.
Example:
A business that offers some kind of subscription to its users.
Its convinient to store some subscription ...
1
vote
2
answers
225
views
Where to determine different behaviour for a child class with feature flags
Let's say that I have a Parent class and a Child class, Parent relies on Child to perform, say, some network request:
class Parent {
...
public init(){
const child = new Child();
const ...
1
vote
4
answers
393
views
Is there any intent behind SRP other than SoC
Edit based on responses so far
I am starting with an edit so as to save future contributors from going down the same path others have.
I am only interested in contributions that stick to the exact ...
1
vote
1
answer
73
views
Querying objects in a list by class type vs string identifier - Best practices
I have a class Character who has a list of Spell objects that he can cast.
abstract class Spell {
name: string
manaCost: number
abstract cast() { ... }
}
class InvisibilitySpell extends ...
4
votes
1
answer
256
views
What is the anti-pattern for modules that group objects of the same type? [closed]
In MVC, I often seen all models in a models.py module, all views in a views.py module, and the controller - you guessed it - in a controller.py module. In other projects, I sometimes see all exception ...
-2
votes
1
answer
103
views
Does my class violate the Single-Responsibility Principle? [duplicate]
Goal: I am learning about SOLID principles and trying to refactor Gilded Rose code, in order to make it fresh and clean.
What I have done: I have created an AbstractItem class as follows, which "...
5
votes
4
answers
497
views
Does my outer class adhere to the Single Responsibility Principle?
I often write front end apps with a generic MVC pattern. I use javascript but this questions is language independent and relates to OOP as a whole. I struggle to understand the SRP principle and I ...
-1
votes
2
answers
224
views
Liskov Substitution Principle Edge Case
I thought I understood Liskov Substitution Principle but then I thought of a case and I wanted to ask the community here if I get it right.
So I read somewhere to check if we have violated Liskov or ...
3
votes
1
answer
268
views
Interface segregation principle
Let's say we have the following business requirements:
We have a list of dishes. They have a name and a list of ingredients. The restaurant owner always wants to have a dish (and only one) marked as ...
2
votes
2
answers
255
views
To maintain SOLID, should data preparation, conversion, and pre-computation for purposes of saving an object, be separate from data persistence layer?
I am facing a common situation where I am saving some values into database from a business object. I am using a relational database and usually I only need to save a few items that are part of the ...
0
votes
1
answer
470
views
Best way to name derived classes when extending its functionality [closed]
I have just recently joined a new company and one of the classes used to log information has not been written properly. So I have been asked to add new functionality to the logger.
Rather than ...
9
votes
4
answers
2k
views
UnsupportedOperationException vs Interface Segregation
This is a follow up/related question to a previous question I asked regarding Interface Segregation. I'll build up on the same example, providing a bit more context.
In the game I'm developing, a card ...
10
votes
3
answers
3k
views
Why is having bloated interfaces an anti pattern?
[I'm using the term interface here with C#'s interface in mind. I won't tag the question with a C# tag because it really isn't a C# question.]
In my work we do unit and integration tests but we don't ...
-1
votes
1
answer
398
views
State machine using SOLID principles. Connecting to real devices
I am trying to implement code for switching states of a couple laboratory devices. My considerations are: how should I link the state objects, state machine object and device manager object to follow ...
0
votes
1
answer
1k
views
Clean code and SOLID principles in WPF/MVVM view models?
I'm currently reading "Clean Code" by Robert Martin (which I should have read years ago), and it's given me a bit of a wakeup call, especially regarding keeping methods and classes small, ...
8
votes
6
answers
454
views
Can Data Abstraction Violate the Interface Segregation Principle?
I'm reading Clean Code
In Chapter 6, Objects and data structure
First talk about Data Abstraction:
Hiding implementation is not just a matter of putting a layer of functions between the variables. ...
3
votes
3
answers
430
views
Is there any redundancy within the scope of SOLID principles?
I have read in an article DIP in the Wild that
"When Robert Martin first discussed the DIP, he equated it a first-class combination of the Open Closed Principle and the Liskov Substitution ...
6
votes
1
answer
3k
views
What are the examples of breaking Liskov Substitution Principle?
I'm aware that there are 4 ways to violate Liskov Substitution from here.
But I'm not sure what these violations would like in practice. Can you show examples of code that breaks the principle for ...
14
votes
5
answers
4k
views
What would be an example of the Liskov Substitution Principle, if you don't use inheritance?
I am reading about the SOLID principles, but it seems like the Liskov-Substitution Principle primarily refers to programs that use inheritance.
From my understanding people are shifting more towards ...
1
vote
0
answers
158
views
How to measure LSP from SOLID Design Principles using NMI, NME and NMO
I do understand the SOLID Principles and LSP, but I want to be able to meausure how well a class diagram obeys to the LSP. Therefor I have found the "Value of LSP" metric, here is a guide ...
0
votes
3
answers
147
views
What should be the optimal management of end user messages in N-tier architecture?
I'm curious about your thoughts on this subject that we have discussed with the people around me. Especially at the architectural stage, in which layer is the most optimal and efficient way to use the ...
3
votes
6
answers
828
views
Liskov Substitution Principle Inconsistency? How to figure out?
Liskov Substitution Principle states:
"if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of the program (...
1
vote
2
answers
99
views
How to improve the design of the code using tools (e.g. git)?
There is a bunch of books that state principles about good code (SOLID, DRY, Design patterns etc.) Those principles are solutions to some problems. An application of those principles could go like ...
1
vote
1
answer
259
views
Why doesn't Laravel actively support Service Classes?
There must be a good reason for this. Laravel's default pattern makes no use of Service Classes... but the Laravel community's best practices is for business logic to be stored in them.
Laravel's ...
1
vote
1
answer
285
views
Wrapping methods without Decorator Pattern or AOP
I have a Dao interface which doesn't have any methods defined.
Then I have multiple classes which implement this interface, like PetDao and HouseDao.
For some of the implementation classes, and some ...
-1
votes
2
answers
124
views
How should I implement this requirement following SOLID?
Assume we had a User class which should hold references for all different kinds of request, let's say at the moment we only have 2 :RequestA and RequestB, both inherit from the base class Request.
Now ...
0
votes
2
answers
112
views
By applying the ISP are we bound to segregating the class too?
So basically the ISP states we should break big interfaces with members that are not cohesive with each other to smaller and more cohesive interfaces, which is very close (if not the same) to what is ...
1
vote
4
answers
221
views
When should a method depend on a data source and NOT have it declared as a parameter?
I was assigned a code review to one of my colleagues.
I posed the following, which I wanted to share here in order to hear whether I am right or wrong.
Consider the following code snippet:
public void ...
0
votes
2
answers
238
views
Should we put behaviors (method) in constant class in Java?
Should we put behaviors (method) in constant class in Java? If not then why? Which clean code practice/principle I am breaking while doing that?
public class TagConstants {
public static final ...
2
votes
2
answers
484
views
Exposing methods for polymorphic children from the aggregate root
Consider the following fictitious toy example:
There is an aggregate root DayChecklist that holds a list of Tasks planned for the day:
class DayChecklist {
String id;
DateTime today;
...
...
1
vote
1
answer
121
views
Any workarounds to split functionality from the class?
Hello everyone)) I am working on the react application, where mobx is being used for the state management. Some time ago I have added such class:
class EventLogStore {
private _eventLogRecord: ...
2
votes
3
answers
326
views
When does a function violate the SRP?
I have a function that was written because several methods used the same sequence of code so an abstraction was useful. The function takes a string representing the path to a registry key and then a ...
1
vote
1
answer
437
views
Clean Architecture: Controller creates a RequestModel instance or implements RequestModel?
I have checked many articles and discussions around the web.
So we have the main diagram:
So we have Controller pointing directly to RequestModel.
So straight to the point, should I:
Create a ...
2
votes
1
answer
594
views
Serialization and SOLID principles
I have several configuration structures that can be JSON serialized and deserialized as follows:
namespace config
{
struct Foo
{
std::string hostname {};
std::string ip_address{};
...
1
vote
1
answer
132
views
OOP Design - Null Object Pattern or design pattern
Consider the code snippet
public interface Car
{
string getColor();
void Drive();
}
public class CarWithAutomaticTransition : Car
{
public string getColor() { return "Red"; }
...