简体   繁体   English

如何命名只调用其他方法的类/方法?

[英]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创建(使用newExtractorConverter ,你应该注入它们(在构造中或在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容器,如SpringPico

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.

相关问题 如何模拟一个方法来调用同一类中存在的其他方法 - How to mock a method that calls other methods that are present in the same class 您如何使方法内部的变量可用于类中的所有其他方法? - How do you make variables made inside a method available to all other methods in a class? 当一个类只调用另一个有多种方法的类上的一个方法时,如何减少耦合? - How to reduce coupling when one class calls only one method on another class that has many methods? Mockito:如何验证一个方法只被调用一次,而忽略对其他方法的调用的精确参数? - Mockito: How to verify a method was called only once with exact parameters ignoring calls to other methods? 如何模拟调用其他私有静态方法的静态方法? - How do I mock static method that calls other private static methods? 如果第三方库类调用super.method,你如何覆盖method()? - How do you override method() in third party library class if it calls super.method? 如何为调用其他方法的方法编写单元测试? - How to write unit tests for method which calls other methods? 你如何命名一个 class 公开序列化和反序列化/编组解组的两种方法 - How do you name a class that exposes both methods for serialzing and deserializing / marshaling unmarshaling 假设您只有类的字符串名称(以及方法名称/参数),那么如何从类中调用静态方法-在Java中 - How to call a static method from a class given that you only have the class's string name (and the method name / parameters) - in Java 显示从方法到其他方法的所有调用 - Show all calls from method to other methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM