SOLID – Single Responsibility
We’re back to our refresher on SOLID principles. Today we are discussing the Principle of Single Responsibility. This is probably my most favorite of all the principles, even though they work hand in hand.
Single Responsibility is a practice of creating objects such that they have only one responsibility. Objects created in this fashion are considered more durable over the long term because they have only one reason to change. Karl Seguin has a good example of performing data access. One responsibility is to retrieve the data from a data store. The second responsibility is to convert the relational data into objects. He demonstrates why these are separate responsibilities, and therefore your code should have two distinct objects to perform each one.
Using the single responsibility principle will cause you to have many more classes. However, the more granular your classes become, the easier it will be to maintain and modify those classes without impacting a large portion of your code. With a modern IDE it is a lot easier to organize your classes so that the volume of separate classes will not become overwhelming to maintain.
A cousin to objects having a single responsibility is the desire for a method to have a single responsibility. This is not really part of SOLID by strict definition. However, when you create your methods with a single responsibility, you will find it much easier to test your code effectively and thoroughly.
That’s a topic I’ll talk about at a later time.
If you are not comfortable with breaking your code out into separate classes with Single Responsibility, I am sure you will find a great deal of success by following through with any research you can do on SOLID.
Do you have a SOLID experience to share with our readers? Great…send it by Email to btaylor@sswug.org.
Cheers,
Ben
$$SWYNK$$
Featured White Paper(s)
How to Use SQL Server’s Extended Events and Notifications to Proactively Resolve Performance Issues
read more)
Featured Script
dba3_ins_Calendar_Article
Modeling Date Logic III: Implementing Business Calendars (by example in MS Sql Server 2000)http://bitonthewire.wpengine.com/see/17379 Dem… (read more)