Ender Wiggin
Ender Wiggin

Reputation: 424

Design of a simple user preference object in java?

I want to design a simple user preference object and its hierarchy. It should be something like

preference
   String Name
   Object Value

booleanPreference
   String Name
   Boolean Value

....

It goes on like that. The preference type changes from class to class. How would I go about implementing such a simple design "abstract class/interface/"?

Upvotes: 0

Views: 178

Answers (1)

duffymo
duffymo

Reputation: 308823

Start with an interface:

public interface Preference<T>  {
    String getName();
    T getValue();
}

Implementation might look like this (not sure about generic; didn't compile to check):

public class PreferenceImpl implements Preference<T> {
    private final String name;
    private final T value;

    public PreferenceImpl(String name, T value) {
        this.name = name;
        this.value = value;
    }

    public String getName() { return this.name; }
    public T getValue() { return this.value; }
}

Upvotes: 5

Related Questions