简体   繁体   English

实用工具类 - 正确的方法是什么?

[英]Utility Class - What is the correct approach?

What is the correct approach for a utility class having all methods with public static. 具有公共静态的所有方法的实用程序类的正确方法是什么。
Should I use final class or abstract class? 我应该使用最终类还是抽象类?
Please give suggestion. 请给出建议。
As for example: 例如:

public final class A{ 
    public static void method(){
        /* ... */
    }
}

OR 要么

public abstract class A{
    public static void method(){
        /* ... */
    }
}

abstract has its own purpose. abstract有其自己的目的。 If you want some of the class functionality implemented by other classes ( override ) then you use abstract. 如果你想要其他类( override )实现的一些类功能,那么你使用abstract。

If it is just utility class, but you don't want other classes subclass it, then I would go with final class. 如果它只是实用程序类,但你不希望其他类继承它,那么我会选择final类。 If utility class has just static methods, any way you can't override them, so it doesn't make difference to have them in non-final class also. 如果实用程序类只有static方法,那么任何方法都不能覆盖它们,所以将它们放在non-final类中也没有区别。

Best approach for creating Utility classes. 创建实用程序类的最佳方法。 If you don't want other classes to inherit it. 如果您不希望其他类继承它。

//final, because it's not supposed to be subclassed
public final class AlertUtils 
{ 

// private constructor to avoid unnecessary instantiation of the class
    private AlertUtils() {
    }

  public static ..(){}//other methods
}

final here makes better sense than abstract . final这里比abstract更有意义。 By marking the class as final , you forbid the extending the class. 通过将类标记为final ,您禁止扩展类。 On the other hand marking the class as abstract is kind of opposite, because abstract class without subclasses doesn't make much sense. 另一方面,将类标记为abstract是相反的,因为没有子类的抽象类没有多大意义。 So it is expected to be extended. 所以预计会延长。

Make the class final and add a private constructor. 使类最终并添加一个私有构造函数。 (This is what classes like java.lang.Math use) (这是像java.lang.Math这样的类使用)

public final class A { 
    private A() {}

    public static void method() {
    }
}

如果你希望其他类使用这个类的功能,那么使它成为抽象的其他类使它成为最终

These are some guidelines I've found: 这些是我发现的一些指导原则:

  • All methods must be public static, so that they cannot be overridden. 所有方法都必须是公共静态的,因此无法覆盖它们。
  • Constructor must be private, so it'll prevent instantiation. 构造函数必须是私有的,因此它会阻止实例化。
  • Final keyword for the class prevents sub-classing. 该类的最终关键字可防止子类化。
  • Class should not have any non-final or non-static class fields. 类不应具有任何非final或非静态类字段。

As you've asked, the class name cannot be abstract (not advisable) -> Which means you are planning to implement it in another class. 正如您所问,类名不能是抽象的(不可取) - >这意味着您计划在另一个类中实现它。 If you want to prevent sub-classing, use final for the class name; 如果要防止子类,请使用final作为类名; If you want to prevent instantiation, use a private constructor. 如果要阻止实例化,请使用私有构造函数。

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

相关问题 在多个微服务之间共享实用程序 class 的最佳方法是什么? - What is best approach to share a utility class between multiple microservices? 哪个更好的实现方法 - 创建多个对象或使用实用程序类 - Which is better Implementation approach - Create multiple objects or use a Utility class 注入Kotlin类的不可变构造函数的正确方法 - Correct approach for immutable constructor injected Kotlin class 没有默认构造函数的测试类的正确方法? - Correct Approach for Testing class having no default contructor? 什么是构建REST角度应用程序的正确方法? - What is correct approach to build REST angular application? 将GWT与持久对象一起使用的正确方法是什么? - What is the correct approach to using GWT with persistent objects? 在私有实用程序类构造函数中使用的首选 Throwable 是什么? - What is the preferred Throwable to use in a private utility class constructor? 在Java(静态类)中重构Utility类的最佳方法是什么 - What is the best way to refactor Utility class in java (static classes) 锁定表中记录的正确/更好的方法是什么? - What should be the correct/better approach for locking a record in table? 从多个片段设置对象值的正确方法是什么? - What is the correct approach to set values of an object from multiple Fragments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM