Skip
Skip

Reputation: 6531

Java - storage structure for the preferences?

What i need to do, is to store a preference stucture in an object. Is there a usual pattern how to save the properties in Java, with the following preconditions:

For Example:

            {CHECKBOXES;"Radio box choice"; { Red, White, Blue}}
            {BOOLEAN;"Hintergrund"; true}} 
            {INT;"Page number"; 34}} 
            {STRING;"App title";"Jenny"}}

Upvotes: 0

Views: 246

Answers (3)

Wivlaro
Wivlaro

Reputation: 1515

You might want to use the Java Preferences API:

http://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html

Upvotes: 1

Henrique Ordine
Henrique Ordine

Reputation: 3337

Create a base JavaBean (POJO) called Preference with the properties type, name and values. Create subclasses of Preference for each preference type, such as CheckboxPref, BooleanPref, etc. Override behavior appropriately in these subclasses. For instance setValue() in CheckBoxPref might have a different behavior than setValue() in IntPref.

Then create another class called PreferenceManager. Store a Collection of Preference there, and expose methods such as loadPreferences(File preferenceFile), setPreferenceValue(String name).

PreferenceManager could also be used as a FactoryMethod for creating the right Preference subclass, according to the Preference Type.

Not sure if that's a Pattern, but that's how I'd do it.

Upvotes: 2

jocelyn
jocelyn

Reputation: 898

You can create a class to represent a property. and use a List in your object.

class Property{

   enum dataType; //create a specific enum
   String name;
   Object value;

}

If you want to go further then you could inherit from this property to add somme type specialised class like BooleanProperty or StringProperty.

Upvotes: 0

Related Questions