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.
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.