java - Refactoring predecessor code -


i'd ask , suggestion how refactor source code receive. here pseudocode of method:

    public void generalmethod(string type) {       inputparameters params = new inputparameters();        if (type.equals("somekey1"){              decodesomekey1(params);           } else if (type.equals("somekey2"){              decodesomekey2(params);           } else if (type.equals("somekey3"){              decodesomekey3(params);           } else if (type.equals("somekey4"){              etc...           }       }     } 

all methods have same input parameters. in first step created new interface , created each method separate class implements created interface.

interface isomeinterfacedecoder {     void decode(inputparameters params); }  class decodesomekey1 implements isomeinterfacedecoder {      @override     public void decode(inputparameters params) {         // implementation     } }  class decodesomekey2 implements isomeinterfacedecoder {      @override     public void decode(inputparameters params) {         // implementation     } }    

then created factory class follows:

class factory {

    isomeinterfacedecoder getdecoder(string type) {          if (type.equals("somekey1"){              return new decodesomekey1();           } else if (type.equals("somekey2"){              return new decodesomekey2();           } else if (type.equals("somekey3"){              return new decodesomekey3());           } else if (type.equals("somekey3"){              etc...           }       }     }  } 

after these changes code looks this:

class someclass {

    factory factory = new factory();      public void generalmethod(string type) {          inputparameters params = new inputparameters();          isomeinterfacedecoder decoder = factory.getdecoder(type);           decoder.decode(params);        } } 

code of method looks better but... method called very often. each time new instance of given class created. can cause performance problems. so, think it's not approach problem. can give me suggestion how should refactor code? in advance help.

instead of having key string, make enum. in enum can implement decode() method this:

public enum mykeyenum {     value1 {         public void decode(inputparameters ip) {             // specific decoding value1         }     },     value2 {         public void decode(inputparameters ip) {             // specific decoding value2         }     }     ...     ;      public abstract void decode(inputparameters ip); } 

now in calling code can this:

public void generalmethod(mykeyenum type) {    inputparameters params = new inputparameters();    type.decode(params); } 

the advantage decode methods in 1 enum, dont need specific class each of decoders. when new value added enum, cannot forget implement decode method (or not compile).


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -