What is the One Class, One Responsibility Principle?

后端 未结 5 1996
广开言路
广开言路 2021-01-12 22:14

I would like to learn about the One Class, One Responsibility principle. I have found some articles about it, but without examples. It would help me if you can giv

5条回答
  •  太阳男子
    2021-01-12 22:52

    I'm not a 100% expert in this design pattern, but here's how I think of it - if I create an object, it is responsible for exactly one thing. If it needs to do something else, but is related to another object, depending on the situation, I would use inheritance, or interfaces.

    It's a concept that seems fairly simple; make sure that a specific object (or method, for that matter) handles one piece of logic. If it handles more, you need another object (or method).

提交回复
热议问题