- Building Enterprise JavaScript Applications
- Daniel Li
- 117字
- 2021-07-23 16:31:32
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 responsibility, open/closed, Liskov substitution, interface 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.