Make an object of a class from a string in java -


i have different classes, each receives strings , convert different html formats. have chooser class have switch cases divert strings e.g. send strings chooser , stylename this

chooser chooser = new chooser(); string text = chooser.format(sometext, stylename); 

the chooser class this:

public string format(string sometext, string stylename) {     switch (stylename) {         case "newstyle":             newstyle ns = new newstyle();             str = ns.refprocess(sometext);             break;         case "anotherstyle":             anotherstyle = new anotherstyle();             str = as.refprocess(sometext);             break;         case "tet_letters":             turk_j_chem tet_letters = new turk_j_chem();             str = tet_letters.refprocess(sometext);             break;     } } 

it there short way? when send stylename string, converts class, make object , send sometext class only?

i assume have interface:

static interface style {     string refprocess(string text); } 

and 3 implemntations:

static class newstyle implements style {     @override     public string refprocess(final string text) {         return "new style of " + text;     } }  static class anotherstyle implements style {     @override     public string refprocess(final string text) {         return "another style of " + text;     } }  static class turk_j_chem implements style {      @override     public string refprocess(final string text) {         return "turk_j_chem's style of " + text;     } } 

then can create enum of classes, , format using enum:

enum styles1 {     new(newstyle.class, "newstyle"),     another(anotherstyle.class, "anotherstyle"),     tet_letters(turk_j_chem.class, "tet_letters");     string str;     class<? extends style> clazz;      styles1(final class<? extends style> clazz, final string str) {         this.clazz = clazz;         this.str = str;     }  }  static string formatusingenum1(final string sometext, final string stylename) {     (final styles1 style : styles1.values()) {         if (style.str.equals(stylename)) {             try {                 return style.clazz.newinstance().refprocess(sometext);             } catch (final exception e) {                 break;             }         }     }     throw new notimplementedexception(stylename); } 

or can populate enum real instances:

enum styles2 {     new(new newstyle(), "newstyle"),     another(new anotherstyle(), "anotherstyle"),     tet_letters(new turk_j_chem(), "tet_letters");     string str;     style style;      styles2(final style instance, final string str) {         style = instance;         this.str = str;     }  }  static string formatusingenum2(final string sometext, final string stylename) {     (final styles2 style : styles2.values()) {         if (style.str.equals(stylename)) {             try {                 return style.style.refprocess(sometext);             } catch (final exception e) {                 break;             }         }     }     throw new notimplementedexception(stylename); } 

but can insert abstract function enum, inline implementation instances:

enum styles3 {     newstyle() {         @override         string refprocess(final string text) {             return "new style of " + text;         }     },     anotherstyle {         @override         string refprocess(final string text) {             return "another style of " + text;         }     },     tet_letters {         @override         string refprocess(final string text) {             return "turk_j_chem's style of " + text;         }     };     abstract string refprocess(string text); }  static string formatusingenum3(final string sometext, final string stylename) {     (final styles3 style : styles3.values()) {         if (style.name().equalsignorecase(stylename)) {             try {                 return style.refprocess(sometext);             } catch (final exception e) {                 break;             }         }     }     throw new notimplementedexception(stylename); } 

enjoy...


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -