Single Responsibility (SOLID Design Principles)

preview_player
Показать описание
SOLID is an acronym for the first five object-oriented design (OOD) principles by Robert C. Martin (also known as Uncle Bob).

These principles establish practices that lend to developing software with considerations for maintaining and extending as the project grows. Adopting these practices can also contribute to avoiding code smells, refactoring code, and Agile or Adaptive software development.

SOLID stands for:

S - Single-responsiblity Principle
O - Open-closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
In this article, you will be introduced to each principle individually to understand how SOLID can help make you a better developer.

Single-Responsibility Principle

The Single-responsibility Principle (SRP) states:

A class should have one and only one reason to change, meaning that a class should have only one job.

For example, consider an application that takes a collection of shapes—circles, and squares—and calculates the sum of the area of all the shapes in the collection...

#solid #singleresponsibility #oop
Рекомендации по теме
Комментарии
Автор

You say - AreaCalculator should be only be concerned with some of the areas of provided shapes. What if we rename AreaCalculator to SumCalculatorOutputter and make 2 public methods: JSon and HTML - result would be the same as AreaCalculator.

dariusv
Автор

intereting. So class createing html and JSOn outputs does single thing? I never understand how this is calculated :D I would think - JSON is one thing, HTML is another thing, so a class is doing 2 things. Yea, you said "one reason to change" which is bit hard to understand :)

dariusv
Автор

And also if we do not need JSON now but only need HTML, so is it really worth to have such small classes with one method? I think if class grows, we then can always extract to new class but while class is small, managable, no problem. Unless we soon expect it to grow, like in comming days or weeks. Otherwise I am not sure if it is worth to have millions of small classes with few lines.

dariusv
Автор

You should use less common examples. These examples provide no more information than every other video on this subject.

Laflamablanca