Enable/Disable pattern?

Hi,

I have a DataModel which holds quite a few fields - about 30. I want to implement this model such that each field also has a notion of being enabled or disabled. By default, i want all fields to be enabled. Then there's the tricky part - setting a field's value may enable or disable other fields. Is there a design pattern out there that can be applied to this type of problem or do i simply resort to messy hard coding of enabling/disabling dependent fields when a particular field is set? Thanks.

Mitch.

[531 byte] By [Mitch_Ja] at [2007-10-3 4:56:28]
# 1

Maybe a combination of a Composite and possibly a Flyweight can do the job:public interface Enableable { // sp?

public boolean isEnabled();

public void setEnabled(boolean enabled);

}

//

public class EnableableComposite implements Enableable {

private boolean enabled;

private List<Enableable> enableables;

//

public void addEnableable(Enableable enableable} {

enableables.add(enableable);

enableable.setEnabled(enabled);

}

// interface implementation:

//

public boolean isEnabled() { return enabed; }

//

pulic boolean setEnabled(boolean enabled) {

this.enabled= enabled;

for (int i= 0; i < enableables.size(); i++)

enableables.get(i).setEnabled(enabled);

}

}

Now associate those Enableables to your fields using elementary

Enableables (not show here) and those Composites. You can use

those Composites at several field positions (the Flyweight).

If you want to 'hard code' the coupling between several field positions

you can even use the elementary Enableables at several positions

making the elemetary Enableables the Flyweight.

kind regards,

Jos

JosAHa at 2007-7-14 23:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Hi JosAH,Thanks for the reply. I just want to let you know that i'm still in the process of solving this problem but have been stuck on a totally different challenge at the moment. Please keep an eye on this topic since i am definitely getting back to it. Thanks
Mitch_Ja at 2007-7-14 23:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Hello,

JosAH, i'm having a little trouble following your example. From what i can understand, an EnableableComposite will contain a list of Enableable instances which are in turn associated with fields. How would i use this for a model such as the following:

public class DataModel {

private String name;

private String surname;

private int age;

private String sex;

private String maritalStatus;

private String spouseName;

private String spouseSurname;

public void setMaritalStatus(String status){

// set the marital status and enable spouseName and

// spouseSurname fields

}

public boolean isSpouseNameEnabled(){

// how do i obtain the enable status of this field?

}

public boolean isSpouseSurnameEnabled(){

// how do i obtain the enable status of this field?

}

}

The example code above kinda shows what i'm after. If the model has it's marital status set then its spouse details are enabled. So if i call model.isSpouseNameEnabled() it should return true. Can your idea of Enableable be applied to this? If so, can u please show me how? Thanks a lot.

Mitch.

Mitch_Ja at 2007-7-14 23:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

You wouldn't use raw String types. You could do something like this:

public class DataModel {

private String name;

private String surname;

private int age;

private String sex;

private EnableableString maritalStatus;

private EnableableString spouseName;

private EnableableString spouseSurname;

public void setMaritalStatus(String status){

maritalStatus = new EnableableString(status);

maritalStatus.addEnableable(spouseName);

maritalStatus.addEnableable(spouseSurname);

maritalStatus.setEnabled(true);

}

public boolean isSpouseNameEnabled(){

return spouseName.isEnabled();

}

public boolean isSpouseSurnameEnabled(){

return spouseSurnameName.isEnabled();

}

}

public class EnableableString extends EnableableComposite

{

private final String value;

public EnableableString(final String value)

{

this.value = value;

}

public String getValue()

{

return value;

}

public String toString()

{

return getValue();

}

}

dubwaia at 2007-7-14 23:01:43 > top of Java-index,Other Topics,Patterns & OO Design...