Could you provide an example of the Single Responsibility Principle?

Technology CommunityCategory: Software ArchitectureCould you provide an example of the Single Responsibility Principle?
VietMX Staff asked 3 years ago

Single Responsibility Principle (SRP) states that a class or a method should only be doing one thing and shouldn’t be any doing anything related. A class should have only one reason to change.

A typical example could a EmailSender class:

  • this should just deal with sending an email out.
  • this should not be responsible for loading the email content from database or even formatting the email content to be sent.