Imagine an application that uses resources such as images or sounds. You may define a common interface for your resources.
interface MyResource {
void load();
void dispose();
// ...
}
Your folder structure may look like the following:
+ Root
|--+ Resources
|--+ Images
|--+ Sounds
|--+ Data
You know that all your resources will be under Root/Resources/
.
This information can be shared and known, not only by your resources, but also by your other application components.
Having this in mind, your interface now becomes:
interface MyResource {
public static final String RESOURCE_ROOT_PATH = "Root/Resources/";
void load();
void dispose();
// ...
}
Your specific implementations, such as an image, may define their own root path, based on the common path for all resources.
class MyImage implements MyResource {
public static final String IMAGE_ROOT_PATH =
MyResource.RESOURCE_ROOT_PATH + "Images/";
...
}
Alternatively, you may look at interfaces like javax.swing.SwingConstants
, which are used to share constants for a certain functionality among implementations.
But, then again, for this sort of thing, I'd rather use an enum
, nowadays.
interface
that defines an http request it may be useful to define constants forGET
andPOST
etc. Having said that, be very wary of the Constant Interface Antipattern.