user2887486
user2887486

Reputation: 89

Switch statements usage and validation best way

I have a situation where I have 2 possible classes and each inheriting the same thing except for one class is inheriting 1 more structure.

Something like:

Class A:    
Race
Name
Sex
Age
Location
Status
ValStat

Class B:    
Race
Name
Sex
Age
Location
Status
ValStat
Nationality

Both A and B are inheriting the Race, Name, Age, Sex, Status, Location and ValStat. B is defining the Nationality

Now I have a validation check based on the Class A or Class B object and I base it off an Enum meaning I have Class A associated with like Local and Class B with like International and it's based off the Location.

So, on the validaton if I check the enum and it's Local then I run the local validation check and if Internional then I base it off a validation. In some of the validations they are the same like associated with Age or Sex. Meaning whether local or International they are still validated.

Right now, I have switch statements.

So I check the Status in the switch and then I check the enum then perform the validation off the enum.

switch (Status)
{
    case Citizen:

         switch (A.Location)
         {

              case West:

                   do common validation using the A object
                   Update the A object ValStat
              break;

              case East:

                  do common validation using the B object
                  do international validation
                  Update the B object ValStat
              break; 
         }
         break;

    case Alien:

         switch (A.Location)
         {

              case West:

                   do alien confirmation using the A object
                   Update the A object ValStat
              break;

              case East:

                  do alien confirmation using the B object 
                  do internation validation
                  Update the B object ValStat
              break; 
         }
         break;

}

Now, from a readability standpoint I do not see it that bad but from a maintenance/update standpoint if I have to add something in the common validation they I need to do it in 2 places. Or if I need to change the Update for the ValStat then I need to do it in 4.

If there another or better way to do something like this?

Upvotes: 1

Views: 555

Answers (1)

Justin Pihony
Justin Pihony

Reputation: 67075

You need to take a look at the strategy pattern.

public abstract class BaseClass
{
    //All your properties
    public virtual Boolean Validate()
    {
        //Common Validation code
    }
}

public class A : BaseClass
{
    public override Boolean Validate()
    {
         return AValidation || base.Validate();
    }
}

//Same for B

Usage

BaseClass base = new A or B
base.Validate();

It might be worth breaking out the validation code to help with SRP and it is usually better to prefer composition over inheritance

public abstract class BaseClass
{
    readonly Validator _validator;
    protected BaseClass(Validator validator){_validator = validator;}
    public Boolean Validate()
    {
        return _validator.Validate();
    }
}

Then, you can abstract the differences in validation away also.

Upvotes: 4

Related Questions