Single responsibility principle
Encyclopedia
In object-oriented programming
, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.
The term was introduced by Robert C. Martin
in an article by the same name as part of his Principles of Object Oriented Design, made popular by his book Agile Software Development, Principles, Patterns, and Practices. Martin described it as being based on the principle of cohesion
, as described by Tom DeMarco
in his book Structured Analysis and Systems Specification.
Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to change. As an example, consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. Second, the format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
The reason it is important to keep a class focused on a single concern is that it makes the class more robust. Continuing with the foregoing example, if there is a change to the report compilation process, there is greater danger that the printing code will break if it is part of the same class.
The single responsibility principle is used in responsibility driven design methodologies like the Responsibility-driven design
(RDD) and the Use Case / Responsibility Driven Analysis and Design (URDAD).
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.
The term was introduced by Robert C. Martin
Robert Cecil Martin
Robert Cecil Martin, known colloquially as "Uncle Bob", is an American software consultant and author. Martin has been a software professional since 1970 and an international software consultant since 1990. In 2001, he initiated the meeting of the group that created Agile software development from...
in an article by the same name as part of his Principles of Object Oriented Design, made popular by his book Agile Software Development, Principles, Patterns, and Practices. Martin described it as being based on the principle of cohesion
Cohesion (computer science)
In computer programming, cohesion is a measure of how strongly-related each piece of functionality expressed by the source code of a software module is...
, as described by Tom DeMarco
Tom DeMarco
Tom DeMarco is an American software engineer, author, teacher and speaker on software engineering topics. He is known as one of the developers of Structured analysis in the 1980s.- Biography :...
in his book Structured Analysis and Systems Specification.
Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to change. As an example, consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. Second, the format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
The reason it is important to keep a class focused on a single concern is that it makes the class more robust. Continuing with the foregoing example, if there is a change to the report compilation process, there is greater danger that the printing code will break if it is part of the same class.
The single responsibility principle is used in responsibility driven design methodologies like the Responsibility-driven design
Responsibility-driven design
Responsibility-driven design is a design technique in Object-oriented programming. It was proposed by Rebecca Wirfs-Brock and Brian Wilkerson who defined it as follows:Responsibility-driven design is inspired by the client/server model...
(RDD) and the Use Case / Responsibility Driven Analysis and Design (URDAD).
See also
- Separation of concernsSeparation of concernsIn computer science, separation of concerns is the process of separating a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors...
- Chain-of-responsibility patternChain-of-responsibility patternIn Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains a set of logic that describes the types of command objects that it can handle, and how to pass off those...
- Cohesion (computer science)Cohesion (computer science)In computer programming, cohesion is a measure of how strongly-related each piece of functionality expressed by the source code of a software module is...
- Open/closed principleOpen/closed principleIn object-oriented programming, the open/closed principle states "software entities should be open for extension, but closed for modification";...
- SOLID - the "S" in "SOLID" stands for the single responsibility principle