我有不同的类,每个都接收字符串并将其转换为不同的html格式。
然后我有一个chooser
类,其中我有转换案例以转移字符串,例如
我将字符串发送到chooser
和stylename
,就像这样
Chooser chooser = new Chooser();
String text = chooser.format(sometext, stylename);
Chooser类是这样的:
public String format(String sometext, String stylename) {
switch (stylename) {
case "NewStyle":
NewStyle ns = new NewStyle();
str = ns.refprocess(sometext);
break;
case "Anotherstyle":
Anotherstyle as = 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;
}
}
它有什么简短的方法吗?这样当我将stylename
作为String
发送时,它会转换为Class
,制作其对象,然后仅将sometext
发送到该类?
答案 0 :(得分:0)
我假设你有一个界面:
static interface Style {
String refprocess(String text);
}
和3个实现:
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;
}
}
然后你可以创建一个类的枚举,并使用枚举格式化
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) {
for (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);
}
或者您可以使用实例填充枚举:
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) {
for (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);
}
但是你可以在enum中插入一个抽象函数,并为所有实例提供内联实现:
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) {
for (final Styles3 style : Styles3.values()) {
if (style.name().equalsIgnoreCase(stylename)) {
try {
return style.refprocess(sometext);
} catch (final Exception e) {
break;
}
}
}
throw new NotImplementedException(stylename);
}
...享受