DEV Community

Discussion on: The S of SOLID in my view.

Collapse
 
efpage profile image
Eckehard

You did not mention derived classes, but the SPR is a strong argument to use them. Assume you have a range of objects or functions all doing similar things. If you write all members of the group separately, you probably need to implement the same code multiple times. If you call a subfunction, it might be hard to meet the different requirements.

If you put the code in an abstract base class, all members of the group will inherit the code but may apply modifications where necessary. Then you will only have one class that is responsible for the behavior of the whole group.

Collapse
 
witerlland profile image
Witerlland Silva

Oh, it's an excellent idea. This text is just my initial annotations, And I never thought so many people would reach me lol. Maybe I can write some text another time, going deeper into the topic.

Collapse
 
efpage profile image
Eckehard • Edited

The topic is much bigger than it seems.

Many systems like the Windows GDI or the android API are build using excessive inheritance. Such systems can contain hundreds of classes, and management would be nearly impossible without a clear structure. Methods introduced are always introduced as early as possible to assure, they are only implemented once.

Here is an image of the android view hierarchy.

Collapse
 
dscheglov profile image
Dmytro Shchehlov

@witerlland actually inheritance is not too much good idea to understand or present SRP.
See my comment: dev.to/dscheglov/comment/2cob9

This text is just my initial annotations, And I never thought so many people would reach me lol

Look, all developers are reflecting SOLID. To write such texts is your way. And it is a really good approach to do that.

Just try to understand comments and discuss it if there is something unclear.

Collapse
 
dscheglov profile image
Dmytro Shchehlov • Edited

@efpage you see, derived classes is not about SRP.

The inheritance is one of the ways to follow OCP (Open/Close Principle) and DIP (Dependency Inversion Principle). And more than -- it is not a best way to respect these principles.

Inheritance is a mechanism of sub-typing, limitted by LSP (Liskov Substitution Principle) that makes SRP not relevant to inheritance: Derived Class MUST DO the same stuff as a Base Class, but in differnet way, preserving safity of substitution.