Should constants ever be placed in an interface?

I know that placing constants in an interface is usually considered bad practice, but ...

I use the Observer pattern to broadcast events from an object to a listener.

interface DownloadListener { public void sendEvent(int eventId); } 

The transmitter uses persistent ints to tell the listener what event has occurred.

 class DownloadTask { public static final int EVENT_DOWNLOAD_STARTED = 1; public static final int EVENT_DOWNLOAD_COMPLETED = 2; //should these go here? DownloadTask(DownloadListener listener) { listener.sendEvent(EVENT_DOWNLOAD_STARTED); } } 

Would it be better to place constants inside the interface? I believe that an interface is a contract between a broadcaster and a listener, and therefore it should contain the details (constants) of this contract.

I am developing for mobile devices (Java 1.3), so, unfortunately, you cannot use an enumeration type.

+4
source share
7 answers

Interfaces describe services, therefore, if a constant is part of the description or definition of a service, it must go to the interface, so each implementation of this interface can reuse them. In your case, for example, constants should go to the interface

+10
source

interface seems like an acceptable place for them.

I think that your "input of constants in the interface is generally considered bad practice", the operator is really applicable only if you use the Anti-pattern with a constant interface .

+3
source

Voo answer extension

 interface DownloadListener { public void sendEvent(MyEnum eventId); public class MyEnum { private final static MyEnum ENUM_1 = new MyEnum(); private final static MyEnum ENUM_2 = new MyEnum(); private MyEnum() { } } public static final MyEnum EVENT_DOWNLOAD_STARTED = MyEnum . ENUM_1; public static final MyEnum EVENT_DOWNLOAD_COMPLETED = MyEnum . ENUM_2 ; } 
+2
source

This is good, but I would really replace int with a portable enum, something like:

 public class MyEnum { public final static MyEnum ENUM_1 = new MyEnum(); public final static MyEnum ENUM_2 = new MyEnum(); private MyEnum() { } } 

it is easy to expand if you need additional information (sometimes debugging is easier if you have a String property and overwrite it in String compared to int ..), and it is safe.

It has the disadvantage that you cannot declare it in the interface definition itself, but you can use the enumeration in the interface method definitions, which should be good enough.

+1
source

Do not forget to clear the constants interface from the anti-template: http://en.wikipedia.org/wiki/Constant_interface

+1
source

There seems to be a better way.

 interface DownloadListener { public void downloadStarted ( ) ; public void downloadCompleted ( ) ; } 
0
source

Historically, constants are placed in interfaces because an interface rarely changes more often than the implementation of specific classes.

In addition, when an interface is implemented and used by many classes, it makes sense to put it in the interface to remove the dependency.

With Java 5 and above, I believe that you should look at the enum data structure for this purpose, and note that an enumeration cannot be placed in an interface. It can be placed in a classroom or on its own.

0
source

Source: https://habr.com/ru/post/1385247/


All Articles