Design Note #1

Single Responsibility Principle (SRP)

Revised: 12/9/2009
Home Courses Code Books Handouts Research SWDev WebDev News OtherLinks SiteDesign

CSE681-SWMA CSE687-OOD CSE691-IP CSE775-DO CSE776-DP CSE778-AWP CSE784-SWS

SG - Design


Single Responsibility Principle (SRP):

Each class should have only one reason to change.

Each module should be responsible for only a single activity.

Examples:

  1. The tokenizer module, in Parser project folder is responsible for extracting tokens from a stream.
  2. tokenizer contains a single class, Toker, responsible for extracting tokens from a stream. Since it refers to the istream class, it can also work with streams derived from istream, like istringstream.

Elaboration:


" If a class has more than one responsibility, then the responsibilities become coupled. Changes to one responsibility may impare or inhibit the ability of the class to meet the others. This kind of coupling leads to fragile designs that break in unexpected ways when changed. "
- Robert C. Martin, Agile Software Development, Prentice-Hall