The Single responsiblity principle (SRP) made practical 

The Single responsiblity principle is one of the fundamental principles in object oriented programming. Simply put, an object should have a single responsibility. This makes our object smaller and focused on doing one thing and do it well 

Responsibly is a vauge term

It’s difficult to really state what is a responsibility of a given class. Can you look at some class and say something like “this class have 5 responsibilities and they are…”?

For example: Did you ever stumbled upon a class named Xmanager, where X is some other entity in the system? This class clearly has one responsibility – managing X. But compare this with the reality of those manager classes – are they simply? Are they short? Does it really feel that they’re doing just one thing?

Another way of looking at the SRP

Let’s explore another way of staying the SRP: A class should have only one reason to change. This implies that a class has only one responsibility since if a class has two different responsibilities then it has more then one reason to change.

So did we make any advancement in this restatement of the SRP? I say that we did!  I encourage you to think of a class that you think might not be doing just one thing, but you’re not sure how to refractor. It’s probably hard to name all responsibilities, but to name different reasons for a class to change? I bet you can name many (even for a moderately sized class).

Advertisements

One thought on “The Single responsiblity principle (SRP) made practical 

Add yours

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: