I have to implement a solution where the code behaves differently according to different conditions.
I created some pseudocode to show it:
public void generalMethod() {
firstMethod();
secondMethod();
commonMethod();
}
public void firstMethod(){
if (condition1){
condition1Method();
} else {
if (condition2){
condition2Method();
} else {
defaultMethod();
}
condition2AndDefaultCommonMethod();
}
}
public void secondMethod(){
if (condition2 && condition1){
condition1Condition2Method();
} else if(condition2) {
condition2Method();
} else {
condition1AndDefaultCommonMethod();
}
}
I decided to use Template Method design pattern for it, because some code is common but some of them varies depending on conditions. Is it the right choice?
I created this small UML diagram for it, but I have a problem, because Condition2Template and DefaultTemplate would share a common method which is not used by Condition1Template or Condition1Condition2Template, so I don't think I should add it to the abstract class Template. Also, Condition1Template and DefaultTemplate share a common method too. Could you please advise on this?