Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to design if some constants are used by multiple classes

Tags:

java

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.

like image 980
mohit Avatar asked Dec 18 '15 05:12

mohit


People also ask

How would you define a group of constants that will be used by many classes?

If constants are logically related to a class, we can just define them there. If we view a set of constants as members of an enumerated type, we can use an enum to define them.

What is the best way to implement constants in Java?

Static − Once you declare a variable static they will be loaded in to the memory at the compile time i.e. only one copy of them is available. Final − once you declare a variable final you cannot modify its value again. Therefore, you can create a constant in Java by declaring the instance variable static and final.

Can abstract classes define constants?

Abstract classes can have constants, members, method stubs (methods without a body) and defined methods whereas interfaces can only have constants and methods stubs. Abstract classes can have constructors but interfaces can't have constructors.

How do you use constants in class?

Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.


3 Answers

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.

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.

like image 72
telkins Avatar answered Oct 16 '22 14:10

telkins


Providing a Constant Util class is the cleanest way.

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

The typical folder heirarchy is like this

com.example.util.ConstantUtil
like image 23
Enzokie Avatar answered Oct 16 '22 14:10

Enzokie


If the contants are dedicated to an api define them there. E.g.

 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. E.g. javax.swing.WindowConstants.

Or is there any other better approach? Note:- I'm looking for best OO technique which would be applicable for this. java

This brings us back to the question how constants are used. Most times they are used to write conditional code. E.g.

 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. E.g. Introduce a Priority class that might implement Compareable ;)

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/. It is about enum misuse, but it also applies to constants.

like image 1
René Link Avatar answered Oct 16 '22 13:10

René Link