Reputation: 6531
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:
it is not known how many preference entries there will exist.
{TYPE;NAME;VALUE}
{TYPE;NAME;VALUE}
{TYPE;NAME;VALUE}
For Example:
{CHECKBOXES;"Radio box choice"; { Red, White, Blue}}
{BOOLEAN;"Hintergrund"; true}}
{INT;"Page number"; 34}}
{STRING;"App title";"Jenny"}}
Upvotes: 0
Views: 246
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
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
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