Trouble implementng an Adapter/Strategy (GoF) design pattern
I have some objects that need various constraints, and each constraint has function for checking the constraint.
So here's a quick detailing of the design I have so far....
Code:
public class Constraint
{
private ConstraintType type;
public static enum ConstraintType
{
type1,
type2,
//...
}
}
public class Constraint1 extends Constraint{ type = Constraint.ConstraintType.whatever}
public class Constraint2 extends Constraint{ type = Constraint.ConstraintType.somethingelse}
etc...
public class ConstrainedObject
{
private Constraint[] constraints;
}
//the set of constraint checking functions...
public class ConstraintChecker
{
public boolean method1();
public boolean method2();
public boolean method3();
}
//this is sort of the Adapter pattern, but maybe strategy.. ?
public class ConstraintChecker
{
private ConstraintChecker validator;
public boolean checkConstraints(ConstrainedObject obj)
{
//for each constraint in obj...
switch (constraint.type)
{
case typeA:
return validator.method1(obj);
case typeB:
return validator.method2(obj);
case typeC:
return validator.method3(obj);
}
}
So, the problem is that I want the Constraint subclasses to be more tightly coupled with the specific constraint type. Would be nice to have it more tightly coupled with the function itself as well, but the functions need to be re-usable also. (like if I have a type that calls method1() and a type calling method2(), I might also want another type that calls both of those methods....
Ideas ?
Thanks