Skip to content

Single Responsibility

The SRP principle can be repharsed as following: "A module should be responsible to one, and only one actor." The module here can be a single Java class, or an interface and its implementation. From the object oriented design we all know what the high cohesion is (we want to design components that are self-contained, independent, and with a single, well-defined purpose). The SRP perfectly match wit it.

Which definition conforms to SRP, and which one violates the SRP principle?

srp-example

Variant A:

  • update() is required by the system administrator or manager actor
  • pickUp() is required by the user who wnats to rent a bike
  • setMaintanenceMode() is required by service person who finds a problem with a bike, which needs to be fixed
  • Who drives the structure and functionality of Bicycle?

Variant B:

  • pickUp() is required by the user who wnats to rent a bike

  • return() is required by the user who wnats to return a bike after he/she finishes a journey

It seems the Variant A violates the SRP principle as multiple actors' requirements have impact on Bicycle class. You can separate the functionaity to dedicated services using the facade pattern. The another solution would be to split responsibilities among multiple modules as part of the DDD/Microservices approach.

References