问题描述
我想了解一等一责任原则。我已经找到一些文章,但没有例子。如果你能给我一个违反这个原则的课程,我会帮助我的。
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 give me an example of a class that violates the principle.
我熟悉一个方法应该只做一件事的想法,比如get和set方法。由于set和get方法都在一个类中实现,所以它不能与一类,一个责任相同。那么这是否意味着该类违反了规则,因为该类有责任设置和获取?
I'm familiar with the idea that a method should do only one thing, for instance get and set methods. It must not be the same as One Class, One Responsibility, because set and get methods are both implemented inside a class. So does this mean the class is violating the rule because the class has responsibilities both to set and to get?
什么是一类,一责任原则 / em>?
What is the One Class, One Responsibility Principle?
推荐答案
我不是这个设计模式的100%专家,但这里是我如何想到的 - 如果我创建一个对象,那么它负责 一件事。如果需要做别的事情,但是与另一个对象有关,根据情况,我会使用继承或接口。
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).
这篇关于一级,一责任原则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!