Posted by & filed under Object Oriented Design, Object Oriented Programming, Programming.

SOLID Principles

This is a series of 5 articles related to solid principles of object-oriented design. Picture credited to Stephen Haunts.

 

Introduction.

 

SOLID principles are a great guideline to design object oriented code. Each of them address a desirable feature in all systems and following them will simplify our code and design.

This is a post with examples and theory as well as some recommendations to read. This is the first of five articles

 

Single Responsibility Principle.

 

Background:

 

This is the first SOLID principle. The term was introduced by Robert C. Martin in his book Agile Software Development, Principles, Patterns, and Practices. 

 

Intent:

 

The intent is to make a class or module to have one, and only one reason to change.

 

What does it solve?

 

It allows a better separation of concerns and allows more cohesive classes and modules thus making modifications less critical by just changing what’s exactly needed at the moment. It also allows small classes that are easy to use, understand and modify.

 

Common Issues:

 

This is a principle that sounds great in theory but takes a lot to do it right in practice. You might end up with lots of too lazy classes with very little responsibility that might evolve to the worst kind of objects “the data buckets with just getters and setters” and lots of services consuming them.

 

Example:

 

Assume you have a system which requires CRUD(Create Read Update Delete) operations for users. Each user have information such as email, full name, birthday, etc.

If we let our user class to handle the persistence such as some ORM(Object Relational Mapping) technologies do, we would end up with a class that have more than one reason to change, when the user updates or add information and when the user is persisted or searched in the system.

bad-user

 

UML Diagram for a poorly designed user class.

This is clearly a bad idea, if we separate the persistence layer to a new class the User class would remain as a class for user information and will only have one reason to change: if the user information is modified or used in a different way and the persistence options could become more flexible.

user-repository

 

Uml diagram for a better designed user class and its repository.

 

Now there’s a clear separation of concerns, the User class is ignorant of the persistence logic and the User repository could make use of different persistence strategies without effort. However, the downside is the User class now is just a data bucket with getters and setters, but chances are the User class will evolve eventually in a more complex class. Another downside with this movement is that there’s a dependency between the User Repository and the User class making the system more complex in implementation but simpler in design and separation of concerns.

The general rule here is to acknowledge the different alternatives and choose the one that best fit your needs. Principles are guidelines not rules but the major advantage of following the principles are a set of common solutions that are agnostic of languages, easy to use and understand for both novices and expert developers and simplicity in our design.

 

Additional Resources

 

Some valuable techniques that we can learn to separate classes in an existing system are the ones described in Martin Fowler’s book Refactoring: Improving the Design of Existing Code such as:

And their counterpart if we find that we separated too much our classes:

Further reading:

 

I recommend the following links if you want to know more about the subject:

Please feel free to ask me more questions or leave suggestions. Hope this was useful and Happy Coding!

Share
  • http://www.itoctopus.com Fadi (itoctopus)

    Unfortunately, the majority of the classes built integrate functionality that should be in a different class – the user class above (first UML) is an example I have seen quite often.

    The user class doesn’t have to end up as setters/getters. Additionally, there’s often a lot of calculation/verification done in these setters/getters functions.

    Thanks for sharing..

    • http://markpatino.com Marco Emmanuel Patiño Acosta

      Thank you, I’m glad it was useful.

  • phersorry

    … Unbelievable , great article, you made the whole thing easier for me. Thanks for sharing!

    Take care – your friend Jessica

  • http://auto-website-promoter.com tutbatmevalty

    by the way I’m waiting for the newest articles next week.

  • http://mass-traffic-software.com Laura felsen

    After browsing a lot for information like this I thought your website is one of the best…
    I wish you a lot of success.

    Laura.

  • http://gagthat.com/hurtlove-jpg/ Get your Funny pictures now.. this is site is good for that if you are looking for that kind of stuff.. thanks for the share!

    Fantastic site. Lots of useful info here. I am sending it to some buddies ans also sharing in delicious. And certainly, thank you for your effort!

  • http://anitoanisioanisiamto.org Letisha Lett

    You ended some first scale factors there. I looked on the internet for the problem and establish on the whole community will exit concurrently with mutually with your website.