How do you define a Single Responsibility?

前端 未结 13 1797
醉酒成梦
醉酒成梦 2021-01-30 09:23

I know about \"class having a single reason to change\". Now, what is that exactly? Are there some smells/signs that could tell that class does not have a single responsibility?

13条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 09:36

    Here are some things that help me figure out if my class is violating SRP:

    • Fill out the XML doc comments on a class. If you use words like if, and, but, except, when, etc., your classes probably is doing too much.
    • If your class is a domain service, it should have a verb in the name. Many times you have classes like "OrderService", which should probably be broken up into "GetOrderService", "SaveOrderService", "SubmitOrderService", etc.

提交回复
热议问题