SRP: Single Responsibility Principle

The S in the SOLID principles is one of the most simple and most important principles.

Let’s take a simple example, one of a clock. A clock might have lot’s of parts, parts that keep time, that tell you what the hour and minute of the day are etc.

A clock might also have other parts such a display to output the time. In OO the display would inherit the other clock parts to obtain the time and then display it. In an analouge clock this might include the clock face, hour hands, minute hands etc. In a digital clock this might include an LED display etc.

Now we ask the question while both displays are are concerned with the display of the time, the analouge clock is not concerned with the digital clocks LED display and the digital clock is not concerned with the analouge clocks hour and minute hands etc. Each display is concerned only with it’s own display types and does not need to know nor bother with anything else. Both displays are not concerned with keeping the time, that is left to other parts of the clock, the only thing the display is concerned with is showing you the time.

If an LED display was concerned with clock hands it would break the SRP principle.

If we take this back to your coding we apply the SRP principle to everything from class to method, making sure that each is focused on one thing.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s