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:
- different Datatypes are can be displayed as settings (Boolean, Int, Checkbox)
- different DataTypes have different number of values (Boolean has one, checkbox has many)
-
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"}}
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.