Questions tagged [solid]
Mnemonics for set of design principles: Single responsibility, Open-closed, Liskov substitution, Interface segregation, Dependency inversion
386
questions
66
votes
8
answers
13k
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 ...
0
votes
1
answer
164
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
198
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
363
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
71
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 ...
3
votes
1
answer
162
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
91
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
314
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
179
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 ...
2
votes
1
answer
229
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
192
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
253
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 ...
-3
votes
1
answer
111
views
How would a SRP `class Car` look like? [closed]
SRP insists that a class should have only a single a reason to change. Please guide me through the following example.
class Car
{
drive_forward();
embark();
disembark();
fuel_up();
}
...
-1
votes
1
answer
344
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
762
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, ...
7
votes
6
answers
411
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. ...
2
votes
3
answers
343
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 ...
5
votes
1
answer
2k
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
3k
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
129
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
130
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 ...
2
votes
6
answers
618
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
94
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 ...
0
votes
1
answer
183
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
164
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
122
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
86
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
218
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
126
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
383
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
119
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
270
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
336
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
457
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
122
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"; }
...
2
votes
2
answers
255
views
What does abstraction mean in Bridge design pattern?
The bridge pattern is defined as 'The Bridge pattern decouples an abstraction from its implementation, so that the two can vary independently'. I get that abstraction here doesn't mean abstract class /...
4
votes
2
answers
130
views
How do I trigger conditional post processing action without violating SRP?
I have a User Model as below, this Model class is in the shared library.
class User {
private long userId;
private String email;
private String userType;
private long departmentId;
...
2
votes
1
answer
126
views
How do refactor a code which picks service based on null checks on field?
I have a bean that has around 10+ fields and it may grow till 30 odd fields. The bean is a contract between applications to share data over an HTTP call. Basically, a downstream application will set ...
2
votes
2
answers
654
views
The most popular definition for LSP seems very contradictory
Out of the 5 SOLID principles, I find LSP to be the most confusing one.
The most popular description of this principle is simply
"A subclass must be replaceable with it's base class without ...
0
votes
2
answers
179
views
Improving dependency inversion principle
Reading a little bit about Functional core & imperative shell pattern, I am wondering if the DI principle can be improved.
What happen if instead of having an Infrastructure layer depending on ...
3
votes
4
answers
735
views
Do Private methods in subclasses violate Liskov Substitution Principle?
I have searched for this question but could not find a good answer for my case.
Suppose I have a SuperClass for a bank loan with properties Amount and InterestRate.
public class BankLoan{
public ...
-3
votes
2
answers
110
views
Do classes that set, get and calculate data follow the S in SOLID?
Supposing I live in a country where every car brand has a different tax rate, and I have a base class called Car
public class Car{
public string CarType { get; set;}
public int Year {get; set;}...
-1
votes
2
answers
149
views
What design/component principle did I apply? How can I further improve it?
I recently refactored a program for code feasibility and maintainability; however, I am totally unaware of what software principle I did implement. I just followed my feeling.
The purpose for this ...
18
votes
9
answers
4k
views
Which object should have the method?
I am trying to create an object model for a user and a chatroom. I'm stuck on where to place certain functionality when the objects collaborate.
At the moment all the functionality for the User is ...
0
votes
4
answers
410
views
Is it an issue to have multiple almost identical interfaces?
Is it an issue to have multiple almost identical interfaces?
In our company we have 9 identical interfaces for 9 document types, like: Book, Contract, Person, etc.
interface IBookInner { Book ...
3
votes
3
answers
344
views
Is my understanding of SOLID principles correct regarding my concrete implementation?
I read everything and its opposite about how to organize its code. Of course, I try to follow the SOLID principles, but since I consider myself quite a beginner, I would need some outside advice on ...
1
vote
1
answer
180
views
Using lambdas to decouple the client code
The ISP says that the client should not depend on things what are not needed. As a solution small focusing interfaces (contracts) can be used:
interface RegisterUser {
User register(String username);...
12
votes
2
answers
2k
views
Does TDD contradict the open-closed principle?
My understanding of the TDD methodology is that (failing) test cases are written promptly after finalizing the requirements.
My understanding of the open-closed principle (in the context of OOP) is to ...