Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

The single responsibility principle

We have pulled out the request handler and migrated it into its own module. However, it is not as modular as it could be; at the moment, the handler serves three functions:

  • Validates the request
  • Writes to the database
  • Generates the response

If you have studied object-orientated design principles, you will undoubtedly have come across the SOLID principle, which is a mnemonic acronym for single responsibilityopen/closedLiskov substitutioninterface segregation, and dependency inversion.

The single responsibility principle states that a module should perform one, and only one, function. Therefore, we should pull out the validation and database logic into their own dedicated modules.