java - How to efficiently share functions between classes without violating the Liskov Substitution Principle -


i have codebase created number of different options allow code perform same process in different way, this:

public class mainfunction {   public void main(string option){      if (option.equals("vanilla mode")){         this.firstfunction();      }else{         this.differentversionoffirstfunction();      }      this.secondfunction();   }    public void firstfunction(){     //first functions code   }    public void secondfunction(){      //second functions code   }    public void differentversionoffirstfunction(){      // different code first functions code produces same type of end result using different method   } } 

this has gotten gradually more , more complex various different options added , code becomes increasingly convoluted.

to resolve had planned create parent object, have children subtlety different variations on parents methods when needed. problem understand violate liskov substitution principle , indeed may have children should sharing same method may not present in parent.

so left having different methods in same class object, doing same job in different ways.

public class mainfunction {   public void main1(){     this.firstfunction();     this.secondfunction();   }    public void main2(){     this.differentversionoffirstfunction();     this.secondfunction();   }    public void firstfunction(){     //first functions code   }    public void secondfunction(){     //second functions code   }    public void differentversionoffirstfunction(){     // different code first functions code produces same type of end result using different method   } } 

i suppose create separate utility class hold various functions, not sure if there more elegant solution?

i don't see how examples violate liskov substitution principle. however, see violate open/closed principle, meaning every time need modify program, edit mainfunction.java file , have chance of affecting possible scenarios of running program. better solution involves having lot of decoupled components when need modify something, modify tiny portion unlikely affect scenarios program might encounter. single responsibility principle about.

as mentioned in answer, scenario seems fit apply strategy pattern. might following:

  1. create interface mainfunction void main() method, without options.
  2. create abstract strategy class such abstractmainfunction public abstract void main() member, without options. class implement mainfunction interface.
  3. create separate implementations of abstractmainfunction needed, e.g. vanillamodemainfunction , differentmainfunction. may find useful keep shared code in abstractmainfunction.
  4. create strategy-switcher class, e.g. mainfunctionservice. have method, public void main(string option) in first example, , have switch statement that:

    mainfunction strategy = defaultfunction; switch(option) {     case "vanilla":          strategy = vanillafunction;          break;     case "different":          strategy = differentfunction;          break; } strategy.main(); 

it looks lot of things in end see how simplifies maintenance , further development.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -