Category: 

What Is the Single Responsibility Principle?

Article Details
  • Written By: Eugene P.
  • Edited By: Angela B.
  • Last Modified Date: 05 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The U.S. Coast Guard led the evacuation of more than 500,000 people from Lower Manhattan on 11 September 2001.  more...

September 27 ,  1940 :  The World War II Axis powers formed with the signing of the Tripartite Pact.  more...

In computer programming and design, the single responsibility principle is a concept that espouses the view that any class in a program should perform only one function in the larger application. This idea partly promotes some of the ideals of object oriented programming, such as encapsulation, because an entire class will be focused on performing a single responsibility and will have little reliance on outside classes. At the same time, it is somewhat antithetical to some of the concepts of early object oriented programming, because the function of a single object is decoupled from the data that the object is handling, meaning many objects in combination might need to be constructed to maintain some central data. The single responsibility principle is the basis for a type of design model known as responsibility-driven design.

An example of the single responsibility principle could take the form of a traditional telephone handset. Some design principles would see the handset as a single object that handles both input from the phone line and the transmission of output from the speaker. Under a single responsibility model, in which a single object should only have a single responsibility, then the handset would consist of several separate objects that each performed a single function, such as only receiving input from the phone line, or only outputting the data through the earpiece.

Ad

One of the advantages that using the single responsibility principle makes possible is a very high level of abstraction and modularity. In the handset example, either the input from the phone line or the way that the signal is outputted to the user can be changed without affecting the neighboring classes as long as they adhere to the same contract for interfacing. Additionally, the reusability of certain components can be very high, because each class is fully encapsulated and relies very little, if at all, on surrounding objects, focusing instead on its one responsibility.

A complication that the single responsibility principle might create is a large amount of classes and objects that all are operating on the same data. This can mean a large amount of overhead and a complicated design process. It also can make debugging a large program difficult, because a single portion of the program could consist of thousands of small class files.

When the single responsibility principle is applied through a responsibility-driven design, the data and the methods used to manipulate the data are separated for design purposes. While this does lead to a certain freedom, encapsulation and modularity in design, it also can generate a number of intermediate patterns and designs that must be used to facilitate a number of classes all attempting to interact with the data at once. On the other hand, if an object’s data and the methods used to manipulate it all are bound together in a single multi-responsibility object, then the code can become harder to modify as systems scale, change or become more complex.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email