简体   繁体   English

这是在Java中执行以下任务的最佳方法

[英]Which is the best way to do the following task in java

I have more than 20 commonly used methods in my application. 我的应用程序中有20多种常用方法。 I would like to move that 20 methods into a common class. 我想将这20个方法移到一个通用类中。

Here my doubt is, define all the methods are static or normal method and create a global object to access that normal methods. 我的疑问是,将所有方法定义为静态方法还是普通方法,并创建一个全局对象以访问该普通方法。

class Common {

public String method1() {........}
public String method2() {........}
public String method3() {........}
public String method4() {........}
public String method5() {........}
...

}

Creating object. 创建对象。

class CommonService {
    private static Common common;

    public static Common getCommon() {
    if(null == common) {
    common = new common();
    }
    return common;
    }
}

If we create all the methods using static means, all 20 methods are stored in PermGen section of the heap. 如果我们使用静态方法创建所有方法,则所有20个方法都存储在堆的PermGen节中。

But if we follow above method means, only one object can be created and stored in java heap. 但是,如果遵循上述方法,则只能创建一个对象并将其存储在Java堆中。

Please clarify which one is the best way. 请说明哪种方法是最好的方法。

"Common functions" is not quite accurate. “通用功能”不太准确。 It really depends on what you want to do, for example when I make some string utils I make StringUtils class and it has what I need. 这实际上取决于您要执行的操作,例如,当我制作一些字符串工具时,我会创建StringUtils类,并且它具有我需要的东西。 Whether to make it static or not depends on data to be processed, if one information might be used more than once for a call then answer is simple - use instances. 是否使其静态化取决于要处理的数据,如果一个信息可能在呼叫中被多次使用,那么答案很简单-使用实例。

You should think about the "best" way in terms of design. 您应该考虑设计方面的“最佳”方法。

If the methods are used for general purposes, making them static is preferable, as you won't have any state to store and you'll save memory this way. 如果将这些方法用于一般目的,则最好将它们设置为static ,因为您无需存储任何状态,并且可以通过这种方式节省内存

You should consider other things before deciding if you want to use static methods in your utility class or not. 在决定是否要在实用程序类中使用static方法之前,应考虑其他因素。 On one hand the utility class will be very easy to test, and it's highly accessible. 一方面,实用程序类非常易于测试,并且易于访问。 On the other hand, it's very hard to mock static methods in your test. 另一方面,在测试中模拟static方法非常困难。

If I have a utility class, I would write it as follows: 如果我有一个实用程序类,则将其编写如下:

public final class Common {

    private Common() { }

    public static int method1() { }
    public static int method2() { }
    // ...

}

If we create all the methods using static means, all 20 methods are stored in PermGen section of the heap. 如果我们使用静态方法创建所有方法,则所有20个方法都存储在堆的PermGen节中。

Methods are not data, but code. 方法不是数据,而是代码。 Where code is stored does not depend on whether a method accepts an implicit this parameter or not. 代码的存储位置不取决于方法是否接受隐式this参数。 If you use the singleton approach, method code will still occupy storage and additionally there will be an instance on the heap. 如果使用单例方法,则方法代码仍将占用存储空间,并且堆上还会有一个实例。

However, all of the above is irrelevant and you are focusing on a completely wrong aspect of your design. 但是,以上所有内容都不相关,因此您将注意力集中在设计的完全错误方面。 What matters is how the decision will affect the code which uses these methods: 重要的是决策将如何影响使用以下方法的代码:

  • static methods are simple and a great choice for pure functions (which don't depend on any external state); 静态方法很简单,是纯函数(不依赖任何外部状态)的理想选择;
  • singletons allow polymorphism, therefore make the methods easier to mock for testing. 单例允许多态,因此使方法更易于模拟以进行测试。

That depends on what the methods do. 这取决于方法的作用。

If those methods are just helper methods, that do not alter state then static is probably better because that way you do not have to create an object every time you want to use one of the methods. 如果这些方法只是辅助方法,并且不会更改状态,则使用static方法可能会更好,因为这样一来,您不必每次使用一种方法都创建对象。 You can just call Common.method() 您可以只调用Common.method()

However, if the object has state then you should rater use object methods and create a new object when you want to use the methods. 但是,如果对象具有状态,那么您应该评估使用对象方法,并在想要使用方法时创建一个新对象。

Hope this helps. 希望这可以帮助。

If sense of this method is "execute pure function" like mathematical sin(x), cos(x) etc static method is the best. 如果此方法的意义是“执行纯函数”,例如数学sin(x),cos(x)等,则静态方法最好。

They belongs to one domain? 他们属于一个域? (range of themats) or to different? (主题范围)还是要不同? (then create more "utility classes" with correct name) (然后使用正确的名称创建更多的“实用程序类”)

If have state (like many people say) maybe better is singleton. 如果有状态(就像很多人说的那样)也许更好是单身。

Shape of the question "i have 20 method in application" and name Common suggest previous (older) design problem, I say "procedural thinking", poor vision of OOP. 问题的形状“我有20种应用方法”和名称常见提示以前的(较旧的)设计问题,我说“过程性思维”,OOP的视野不好。

Hard to say without code. 没有代码很难说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM