[英]How do you name a class/method that only calls other methods?
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. 如你所见,“someMethod”-Method会调用提取,转换和添加。 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?
如果你在责任之后命名这个方法会是什么?
Well since you seem to add to a queue and you don't return anything I'd call it addToQueue
. 好吧,因为你似乎添加到一个队列,你没有返回任何我称之为
addToQueue
。 The fact that you convert + extract is implementation detail that I don't think needs to be exposed. 转换+提取的事实是我认为不需要公开的实现细节。
What about processAndQueueMessage
? 那么
processAndQueueMessage
呢?
Also (not related), you shouldn't create (using new
) the Extractor
and Converter
in your SomeClass
, you should rather inject them (at construction or in setters), and use interfaces to them. 另外(不相关),你不应该在
SomeClass
创建(使用new
) Extractor
和Converter
,你应该注入它们(在构造中或在setter中),并使用它们的接口。 That will make it easier to test, and reduce coupling between implementations. 这将使测试更容易,并减少实现之间的耦合。
// Assuming Converter and Extractor are interfaces to the actual implementations
public class SomeClass {
private final Extractor extractor ;
private final Converter converter;
private Queue queue = new Queue();
public SomeClass(Extractor extractor, Converter converter) {
this.converter = converter;
this.extractor = extractor;
}
public void someMethod(List<Container> list) {
Container tmp = extractor.extract(list);
String result = converter.convert(tmp);
queue.add(result);
}
}
And you create it using: 你使用以下方法创建它:
final SomeClass myProcessor = new SomeClass(new MyExtractorImplementation(), new MyConverterImplementation());
(Or use a DI container, like Spring
or Pico
) (或使用DI容器,如
Spring
或Pico
)
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"?
那么“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". “Builder”的替代品可能是“SomeKindOfCreator”。 Obviously you would name it based on whatever it is your class is actually creating.
显然,你会根据你的班级实际创建的内容来命名它。
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. 如果你想让这个名字变得非常通用,我会选择addToQueue()或populateQueue(),因为在这个对象中获取内容似乎是方法的重点。
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.
或者可能不是。
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").
如果你不能提出一个简洁的名字,那么你可以使用通用/中性名称(如“过程”)或使用完全虚假的东西(如“sploddify”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.