I am designed to maintain a system that takes in account the value of three variables to determine which action it will take.
I want to refactor it to use a design pattern, but could not find one suitable for it needs.
To explain the situation, I will use as an example a gym system.
Every gym user has a TYPE_OF_CONTRACT, that could be:
- PLATINUM_MEMBERSHIP
- GOLD_MEMBERSHIP
- SILVER_MEMBERSHIP
The gym has some GYM_CLASSES:
- WEIGHT_LIFTING
- BODY_BALANCE
- STEP
- SPINNING
- ZUMBA
- PERSONAL_TRAINING
Every gym user has a PHYSICAL_CONDITION
- NO_RESTRICTIONS
- OVER_65
- LIMITED_MOBILITY
- MEDICAL_CONDITION
- BELOW_18
For each combination of these three characteristics, a arbitrary set of actions should be executed. For example:
if PLATINUM_MEMBERSHIP + PERSONAL_TRAINING + OVER_65:
- medical approval needed
- signed form
if GOLD_MEMBERSHIP + PERSONAL_TRAINING + OVER_65:
- medical approval needed
- signed form
- extra monthly fee
if SILVER_MEMBERSHIP + PERSONAL_TRAINING + OVER_65:
- refuse subscription
if (any membership) + STEP + MEDICAL_CONDITION:
- medical approval needed
- signed form
if PLATINUM_MEMBERSHIP + WEIGHT_LIFTING + LIMITED_MOBILITY:
- medical approval needed
- signed form
- dedicated staff member assist
And so on.
The combination of characteristics can have a set of actions, that are not exclusive and not all of the combinations are ensured.
The legacy code uses nested switches as implementation. Example:
switch (contractType):
case PLATINUM_MEMBERSHIP:
switch (gymClass):
case (PERSONAL_TRAINING):
switch (physicalCondition):
case (OVER_65):
requiresMedicalApproval();
requiresSignedForm();
...
My problem is:
- there are 3 conditions that combines to define a set of rules;
- these rules are not necessarily unique;
- not every combination defines a set;
I refactored a little using extract method technique and cleaning the code a little, but could not get rid of the 3 switches.
I wish to use design patterns to improve the design but so far I was unsuccessful.
I thought about polymorphism and Strategy, but could not figure a way to use any of them.
I also researched in google but haven't found anything that I could use.
What do you suggest?
Thank you.
Aucun commentaire:
Enregistrer un commentaire