简体   繁体   English

如何设计多个类是否使用某些常量

[英]how to design if some constants are used by multiple classes

I've few common constants which are used by multiple classes. 我很少有多个类使用的常见常量。

What's the most effective way to design in this case: 在这种情况下,最有效的设计方法是:

  1. Should I redefine the constants in each class? 我应该在每个类中重新定义常量吗?
  2. Or should I separate such constants in a public class, and use the constants (in separate class) within each class? 还是应该在公共类中分离此类常量,并在每个类中使用这些常量(在单独的类中)?

Or is there any other better approach? 还是还有其他更好的方法?

Note:- I'm looking for best OO technique which would be applicable for this. 注意:-我正在寻找适用于此的最佳OO技术。

Constants should be strictly related to some type, they shouldn't just "exist". 常量应该与某种类型严格相关,而不仅仅是“存在”。 A Constants class may seem convenient, but it will soon become unmaintainable, not to mention many consider it an antipattern. Constants类似乎很方便,但很快将变得难以维护,更不用说许多人将其视为反模式。

It's hard to suggest improvements without seeing your code, but it seems like you need to rethink your design if you find yourself needing the same constants defined in a few different classes outside of the scope of a type. 很难在不看代码的情况下提出改进建议,但是如果您发现自己需要在类型范围之外的几个不同类中定义的相同常量,则似乎需要重新考虑设计。

Providing a Constant Util class is the cleanest way. 提供Constant Util类是最干净的方法。

class ConstantUtil {
    public static final int MAX_SIZE = 1<<10;
}

The typical folder heirarchy is like this 典型的文件夹层次结构是这样的

com.example.util.ConstantUtil

If the contants are dedicated to an api define them there. 如果争用者专用于api,请在此处定义它们。 Eg 例如

 public interface TaskService {

     public static final int PRIORITY_LOW = -1;
     public static final int PRIORITY_NORMAL = 0;
     public static final int PRIORITY_HIGH = 1;

     public void schedule(Task task, int priority);
 }

If constants are not releated to a single api define a constants interface. 如果常量没有关联到单个api,则定义一个常量接口。 Eg javax.swing.WindowConstants . 例如javax.swing.WindowConstants

Or is there any other better approach? 还是还有其他更好的方法? Note:- I'm looking for best OO technique which would be applicable for this. 注意:-我正在寻找适用于此的最佳OO技术。 java 爪哇

This brings us back to the question how constants are used. 这使我们回到了如何使用常量的问题。 Most times they are used to write conditional code. 大多数情况下,它们用于编写条件代码。 Eg 例如

 public class TaskServiceImpl implements TaskService {

     private List<Task> lowPriority = new ArrayList<Task>();
     private List<Task> normalPriority = new ArrayList<Task>();
     private List<Task> highPriority = new ArrayList<Task>();

     public void schedule(Task task, int priority){
         if(priority == PRIORITY_HIGH ){
             highPriority.add(task);
         } else if(priority == PRIORITY_LOW ){
             lowPriority.add(task);
         } else if(priority == PRIORITY_NORMAL){
             normalPriority.add(task);
         } else {
             ....
         }
     }
 }

In this case find out what the purpose of the constants is. 在这种情况下,找出常量的目的是什么。 In the example above the purpose is to group the tasks or if you think further to order them for execution. 在上面的示例中,目的是对任务进行分组,或者如果您进一步考虑将其排序以便执行。 Move that logic to an own class. 将该逻辑移到自己的类上。 Eg Introduce a Priority class that might implement Compareable ;) 例如,介绍一个可能实现可CompareablePriority类;)

You can also take a look at my blog about type-switches https://www.link-intersystems.com/blog/2015/12/03/enums-as-type-discriminator-anti-pattern/ . 您也可以看一下我有关类型开关的博客https://www.link-intersystems.com/blog/2015/12/03/enums-as-type-discriminator-anti-pattern/ It is about enum misuse, but it also applies to constants. 这是关于枚举的滥用,但也适用于常量。

You can try to create a class which contains all the constants(ie, the second approach). 您可以尝试创建一个包含所有常量的类(即第二种方法)。

For example: 例如:

classname :  MyConstantsClass.java    
public static final String SOME_CONSTANT="value";

and then use it like 然后像

MyConstantsClass.SOME_CONSTANT

As some have suggested to use interface to create constants then I don't think that it would be a good choice. 正如有人建议使用接口创建常量那样,我认为这不是一个好选择。 As using interface to create constants have certain disadvantages as well: 由于使用接口创建常量也有某些缺点:

  • The usage of an interface does not allow to implement a mechanism for converting the constants to a visible/human readable representation. 接口的使用不允许实现将常量转换为可见/人类可读表示的机制。
  • If the constants are an "implementation detail", an interface might not be the natural place for the value (CodeSmells, ListenToTheCode). 如果常量是“实现细节”,则接口可能不是该值的自然位置(CodeSmells,ListenToTheCode)。
  • Due to Java compiler optimization, complete code recompilation of all classes using the constants is necessary if a constant changes. 由于Java编译器的优化,如果常量发生更改,则必须使用常量对所有类进行完整的代码重新编译。 Just changing the interface and recompiling it does not work (this is, however the case with all constants defined as 'static final') 仅更改接口并重新编译它是行不通的(但是,所有常量都定义为“ static final”的情况)

Please refer: 请参考:

Constant class : 常量类:

 interface Constants {
    int CONSTANT_ONE = 0;
    int CONSTANT_TWO = 1;
    int CONSTANT_THREE = 2;

    String BASE_PATH = "YourPath";
 }

And usage: 和用法:

if (someValue == Constants.CONSTANT_ONE) {

 }

Edit: In most cases use interface instead of class if there is no need to implement this interface by some class, then it is no need to add this public static final , because by default it is public static final so your code looks more clean. 编辑:在大多数情况下使用interface ,而不是class ,如果没有必要实施一些类这个接口的话,就不需要添加这种public static final ,因为默认情况下它是public static final使你的代码看起来更干净。

Design a class like AppUtil.java and and define your constants public static final like below: 设计一个类似AppUtil.java的类,并定义您的常量public static final,如下所示:

public class AppUtil
 {
   public static final String IMAGE_STATUS="0";

 } 

and when you want to use variable use . 以及当您想使用变量use时。 like below: 如下所示:

AppUtil.IMAGE_STATUS

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

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