mercredi 27 juillet 2016

Class level design for validation engine with 2 independent entities

I have a use case where there will be 3 kind of data

  1. Business_Enrollment_Program (lets denote them with BEP_1, BEP_2 ...)
  2. Validation_Rule (lets denote them with R1, R2 ...)
  3. Transaction_Type (lets denote them TT_1, TT_2 ...). This is an entity class having some attributes. On these entities Validation_Rule need to be executed.

Transaction_Type entities will look something like

public TT_1 {
private Business_Enrollment_Program;
private COMMON_FIELD_1;
private COMMON_FIELD_2;
private TT_1_SPECIFIC_FIELD;
}

public TT_2 {
private Business_Enrollment_Program;
private COMMON_FIELD_1;
private COMMON_FIELD_2;
private TT_2_SPECIFIC_FIELD;
}

Now i have 2 requirement while executing Validation rules:

  1. Set of Validation_Rule that need to be executed depends on the Transaction_Type and it's Business_Enrollment_Program. That means for TT_1 enrolled under BEP_1 we might need to execute (R1,R2) rules but for TT_1 enrolled under BEP_2 we might need to execute (R1,R3) rules.

  2. Behavior of rule will depend on Transaction_Type and it's Business_Enrollment_Program. That means for TT_1 enrolled under BEP_1 behavior of rule R1 might be different compared to TT_1 enrolled under BEP_2

For rules i can create a structure like below:

public interface Rule <T> {
    public boolean execute(T transactionType);
}

public class R1_For_TT_1 implements Rule<TT_1> {
    public boolean execute(TT_1 transactionType) {
       //Do something here
    }
}

public class R1_For_TT_2 implements Rule<TT_2> {
    public boolean execute(TT_2 transactionType) {
       //Do something here
    }
}

And i can execute the rules like below

public processTransaction(T transactioType) {
    if(t instanceof TT_1) {
        R1_For_TT_1.execute(t);
    }
    else if (t instanceof TT_2) {
        R1_For_TT_1.execute(t);
        R2_For_TT_1.execute(t);
    }
}

Issue with this approach is i am not meeting my 2nd requirement where i wanted behavior of rule to depend on Transaction_Type and it's Business_Enrollment_Program.

Any idea how can i arrange my classes and entities so that both of my requirements are fulfilled elegantly?

Aucun commentaire:

Enregistrer un commentaire