mohit
mohit

Reputation: 6044

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.

Upvotes: 10

Views: 2289

Answers (6)

René Link
René Link

Reputation: 51333

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.

Upvotes: 1

Arun kumar
Arun kumar

Reputation: 1894

Design a class like AppUtil.java and and define your constants public static final like below:

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

 } 

and when you want to use variable use . like below:

AppUtil.IMAGE_STATUS

Upvotes: -1

Bahramdun Adil
Bahramdun Adil

Reputation: 6079

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.

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You can try to create a class which contains all the constants(i.e, 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).
  • Due to Java compiler optimization, complete code recompilation of all classes using the constants is necessary if a constant changes. Just changing the interface and recompiling it does not work (this is, however the case with all constants defined as 'static final')

Please refer:

Upvotes: 0

telkins
telkins

Reputation: 10540

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.

Upvotes: 3

Enzokie
Enzokie

Reputation: 7415

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

Upvotes: 1

Related Questions