Questions tagged [solid]

Mnemonics for set of design principles: Single responsibility, Open-closed, Liskov substitution, Interface segregation, Dependency inversion

In object-oriented computer programming, the term SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible and maintainable. The principles are a subset of many principles promoted by Robert C. Martin. Though they apply to any object-oriented design, the SOLID principles can also form a core philosophy for methodologies such as agile development or adaptive software development. The theory of SOLID principles was introduced by Martin in his 2000 paper Design Principles and Design Patterns, although the SOLID acronym itself was introduced later by Michael Feathers.

Concepts

Single responsibility principle

a class should have only a single responsibility (i.e. changes to only one part of the software's specification should be able to affect the specification of the class).

Open/closed principle

"software entities … should be open for extension, but closed for modification."

Liskov substitution principle

"objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program." See also design by contract.

Interface segregation principle

"many client-specific interfaces are better than one general-purpose interface."

Dependency inversion principle

one should "depend upon abstractions, [not] concretions."


More to read: SOLID

385 questions
213
votes
16 answers

When using the Single Responsibility Principle, what constitutes a "responsibility?"

It seems pretty clear that "Single Responsibility Principle" does not mean "only does one thing." That's what methods are for. public Interface CustomerCRUD { public void Create(Customer customer); public Customer Read(int CustomerID); …
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
159
votes
6 answers

SOLID Principles and code structure

At a recent job interview, I couldn't answer a question about SOLID -- beyond providing the basic meaning of the various principles. It really bugs me. I have done a couple of days worth of digging around and have yet to come up with a satisfactory…
S-Unit
  • 1,397
  • 4
  • 10
  • 9
150
votes
12 answers

Is this a violation of the Liskov Substitution Principle?

Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure the option to close a started ProjectTask is never…
Paul T Davies
  • 3,144
  • 2
  • 22
  • 22
123
votes
12 answers

Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?

If a Square is a type of Rectangle, than why can't a Square inherit from a Rectangle? Or why is it a bad design? I have heard people say: If you made Square derive from Rectangle, then a Square should be usable anywhere you expect a…
user793468
  • 1,363
  • 2
  • 10
  • 12
80
votes
8 answers

How can a class have multiple methods without breaking the single responsibility principle

The Single responsibility principle is defined on wikipedia as The single responsibility principle is a computer programming principle that states that every module, class, or function should have responsibility over a single part of the…
Goose
  • 1,858
  • 2
  • 15
  • 27
70
votes
4 answers

Does following SOLID lead to writing a framework on top of the tech stack?

I like SOLID, and I try my best to use and apply it when I'm developing. But I can't help but feel as though the SOLID approach turns your code into 'framework' code - ie code you would design if you were creating a framework or library for other…
Igneous01
  • 2,343
  • 2
  • 15
  • 18
61
votes
8 answers

LSP vs OCP / Liskov Substitution VS Open Close

I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more). the open/closed principle states "software entities (classes, modules, functions, etc.) should be…
60
votes
5 answers

What are the design principles that promote testable code? (designing testable code vs driving design through tests)

Most of the projects that I work on consider development and unit testing in isolation which makes writing unit tests at a later instance a nightmare. My objective is to keep testing in mind during the high level and low level design phases itself.…
CKing
  • 1,012
  • 3
  • 10
  • 14
55
votes
10 answers

Why would the 'final' keyword ever be useful?

It seems Java has had the power to declare classes not-derivable for ages, and now C++ has it too. However, in the light of the Open/Close principle in SOLID, why would that be useful? To me, the final keyword sounds just like friend - it is legal,…
Vorac
  • 7,073
  • 7
  • 38
  • 58
52
votes
5 answers

IOC Containers break OOP Principles

What is the purpose of IOC Containers? The combined reasons for it can be simplified to the following: When using OOP/SOLID Development principles, Dependency Injection gets messy. Either you have the top-level entry points managing dependencies…
Suamere
  • 1,098
  • 1
  • 11
  • 22
51
votes
5 answers

Where should you put constants and why?

In our mostly large applications, we usually have a only few locations for "constants": One class for GUI and internal contstants (Tab Page titles, Group Box titles, calculation factors, enumerations) One class for database tables and columns (this…
Tim Meyer
  • 833
  • 1
  • 8
  • 14
50
votes
7 answers

Managing and organizing the massively increased number of classes after switching to SOLID?

Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that at least resembles SOLID, but we're not quite…
JD Davis
  • 1,367
  • 1
  • 15
  • 23
50
votes
8 answers

When NOT to apply the Dependency Inversion Principle?

I am currently trying to figure out SOLID. So the Dependency Inversion Principle means that any two classes should communicate via interfaces, not directly. Example: If class A has a method, that expects a pointer to an object of type class B, then…
Vorac
  • 7,073
  • 7
  • 38
  • 58
49
votes
3 answers

Using a "Pass-through (God) Service" is bad, right?

My team has developed a new service layer in our application. They created a bunch of services that implement their interfaces (E.g., ICustomerService, IUserService, etc). That's pretty good so far. Here is where things get a bit strange: We have a…
47
votes
2 answers

Equivalent of SOLID principles for functional programming

I've found the SOLID principles quite useful when thinking about object-oriented design. Is there a similar / equivalent set of language-agnostic principles tailored for functional programming?
mikera
  • 20,617
  • 5
  • 75
  • 80
1
2 3
25 26