如何命名只调用其他方法的类/方法?
假设我遵循单一职责原则,并且我有以下课程。
public class Extractor {
public Container extract(List<Container> list) {
... some extraction
}
}
public class Converter {
public String convert(Container container) {
... some conversion
}
}
正如您所看到的,它遵循原则,所有类/方法的名称都说明了它们的作用。现在我有另一个类,它有这样的方法。
public class SomeClass {
private Extractor extractor = new Extractor();
private Converter converter = new Converter();
private Queue queue = new Queue();
public void someMethod(List<Container> list) {
Container tmp = extractor.extract(list);
String result = converter.convert(tmp);
queue.add(result);
}
}
正如您所看到的“someMethod”方法确实调用了提取、转换和添加。我现在的问题是,如何调用这样的类/方法?它实际上并不是提取、转换或添加,而是调用这些? 如果您按照该方法的职责来命名该方法,那会是什么?
Say I follow the Single Responsibility Principle and I have the following classes.
public class Extractor {
public Container extract(List<Container> list) {
... some extraction
}
}
public class Converter {
public String convert(Container container) {
... some conversion
}
}
As you can see it's following the principle and all the names of the classes/methods tell what they do. Now I have another class that has a method like this.
public class SomeClass {
private Extractor extractor = new Extractor();
private Converter converter = new Converter();
private Queue queue = new Queue();
public void someMethod(List<Container> list) {
Container tmp = extractor.extract(list);
String result = converter.convert(tmp);
queue.add(result);
}
}
As you can see the "someMethod"-Method does call extract, convert and add. My question is now, how do you call such a class/method? It's not actually extracting, converting or adding but it's calling those?
If you name the method after its responsibility what would that be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望名称真正通用,我会使用 addToQueue() 或 populateQueue() ,因为将某些内容放入该对象似乎是该方法的重点。
但实际上在这个级别上,我会根据它试图完成的业务逻辑来命名它,在这种情况下,名称实际上取决于它的用途。
如果您无法想出一个好名字,则表明您的程序抽象相当任意/人为,并且可能暗示可能有更好的方法来做到这一点。或者也许不是。
If you want the name to be really generic, I'd go with addToQueue() or populateQueue() since getting something into that object seems to be the point of the method.
But really at that level I'd call it by what business logic it's trying to accomplish, in which case the name really depends on what it's being used for.
If you can't come up with a good name, it is an indication that your procedural abstraction is rather arbitrary / artificial, and a possible hint that there might be a better way to do it. Or maybe not.
听起来像是某种构建器类。您获取一种格式的数据,对其进行转换,然后创建某种输出格式。那么“SomethingSomethingBuilder”怎么样?
我假设有人对我投了反对票,因为我忘记为该方法提供一个好名字。对此感到抱歉。
因此,此方法将增量数据添加到您的构建器类中。我会称其为“Add”、“AddData”或“Push”(我可能会选择“push”,因为这在许多标准类中具有非常相似的含义)。
“Builder”的替代方案可能是“SomeKindOfCreator”。显然,您可以根据您的班级实际创建的内容来命名它。
Sounds like some kind of builder class. You get data in one format, convert it and then create some kind of output format. So how about "SomethingSomethingBuilder"?
I'm assuming someone downvoted me because I forgot to provide a good name for the method. Sorry about that.
So this method adds incrementally data into your builder class. I would call it, "Add", "AddData" or "Push" (I'd probably go with push because that has very similar meaning in many standard classes).
Alternative to "Builder" could potentially be "SomeKindOfCreator". Obviously you would name it based on whatever it is your class is actually creating.
好吧,既然您似乎添加到队列中并且没有返回任何内容,我将其称为
addToQueue
。您转换+提取的事实是我认为不需要公开的实现细节。Well since you seem to add to a queue and you don't return anything I'd call it
addToQueue
. The fact that you convert + extract is implementation detail that I don't think needs to be exposed.processAndQueueMessage
怎么样?另外(不相关),您不应该在
SomeClass
中创建(使用new
)Extractor
和Converter
,您应该注入它们(在构造时或在设置器中),并使用它们的接口。这将使测试变得更容易,并减少实现之间的耦合。您可以使用以下方法创建它:(
或者使用 DI 容器,例如
Spring
或Pico
)What about
processAndQueueMessage
?Also (not related), you shouldn't create (using
new
) theExtractor
andConverter
in yourSomeClass
, you should rather inject them (at construction or in setters), and use interfaces to them. That will make it easier to test, and reduce coupling between implementations.And you create it using:
(Or use a DI container, like
Spring
orPico
)您要做的就是考虑方法调用序列的复合含义,将其转换为简洁的动词或动词短语,并将其用作名称。如果您无法想出一个简洁的名称,那么您可以使用通用/中性名称(例如“process”)或使用完全虚假的名称(例如“sploddify”)。
What you do is think about the composite meaning of the sequence of method calls, turn that into a concise verb or verb phrase and use that as the name. If you can't come up with a concise name then you could use a generic / neutral name (like "process") or use something completely bogus (like "sploddify").