Single responsibility principle

Check out the SOLID series:

Let us illustrate one of the fundamental rules of OOAD, Single responsibility principle in this post.

SRP: Every class should have a single responsibility. If we have a class ‘Person’, which can be a fundamental class for a software; should contain information and not the other operations.

In this example class, we have used the class just to store the values from the instance of the class. If we have to allow any operation, for ex CRUD (create, read, update and delete), we have to write another class ex PersonManager class which will do this operation.

One can create a GOD class/method, which will have all the functionality in the same large file. But in this way the software will be tightly coupled. Hence, for adding new feature or re-usability of the code will be very difficult and might leads to the duplicity of the code.

In basic class designing, encapsulation is implemented by making the member variables private and the accessing methods get/set as public. Thus a class can be controlled and data can secured from the outer world. A small functions not only prevents the modularity of the logic implemented, but also can be easily extended without rewriting the existing logic.

Single responsibility principle is a first step to move to SOLID programming by avoiding the STUPID methodology.

Tagged with: , , , , ,
Posted in Java, software design
2 comments on “Single responsibility principle
  1. nikhilvartak says:

    Is this what they call “POJO”?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 353 other followers

%d bloggers like this: