Blog Stats
  • Posts - 28
  • Articles - 0
  • Comments - 21
  • Trackbacks - 2

 

Single Responsibility Principle

For those not familiar with the single responsibility principle here is a good description from the c2 wiki.
  • Each responsibility should be a separate class, because each responsibility is an axis of change.
  • A class should have one, and only one, reason to change.
  • If a change to the business rules causes a class to change, then a change to the database schema, GUI, report format, or any other segment of the system should not force that class to change.
It is a counter-intuitive but valuable principle of object-oriented design. I came across this example today:

My application is using  a service layer between the UI and the domain model. On my ProductService I have a method that is used to add images to products. When an image is added it must be saved in three different sizes. The old me would have included the image resizing code in the ProductService, that is the intuitive thing to do.

Following the single responsibility principle I instead created an ImageResizingService class to handle the resizing. So now my ProductService is dependant upon ImageResizingService (or at least its interface) but it has no knowledge of how to resize an image, and my Image class remains gloriously dependency free.


Feedback

# re: Single Responsibility Principle

Gravatar This is an important principle that is often ignored. While it can be very difficult to implement 100% (and often not really worth the effort), it should ALWAYS be considered during the design, and implemented to the highest practical level.

This roughly cooresponds to the separation of "Intrinsic" and "extrinsic" functionallity. In plain language, "Separate what an object DOES, from what can BE DONE TO an object".

The latter is scheduled for discussion on my blog in the very near future as part of the ongoing series on developing agile software (which is completely different than agile software development)... 4/24/2008 7:44 AM | TheCPUWizard

Post a comment





 

Please add 7 and 2 and type the answer here:

 

 

Copyright © Liam McLennan