Suppose I have classes A,B that each of them extends some class X. And I want a method to create A or B based on some parameter value(value is a result of some other logic).
Can I do it without a switch statement?
i.e.:
class X {...}
class A extends X {...}
class B extends X {...}
So naive would be to make a class:
class Z {
X createObject(int type) {
switch(type)
case 1: return new A();
...
case 2: return new B();
}
答案 0 :(得分:0)
Yes, you can do it without a switch statement. I suggest using either an array or Map
and Supplier
.
Map<Integer, Supplier<X>> map = new HashMap<>();
map.put(1, A::new); // () -> new A()
map.put(2, B::new); // () -> new B()
X createObject(int type) {
Supplier<X> supplier = map.get(type);
if (supplier == null) return null;
return supplier.get();
}
答案 1 :(得分:0)
You can of course do it without a switch
statement.
If you have only a few cases you could use the ternary operator.
public static X createObject(int type) {
return type == 1 ?
new A() :
type == 2 ?
new B() :
null;
}
You could use also a more generic approach:
private static final Map<Integer, Supplier<X>> FACTORIES;
static {
FACTORIES = new HashMap<>();
FACTORIES.put(1, A::new);
FACTORIES.put(2, B::new);
}
public static X createObject(int type) {
return Optional.ofNullable(FACTORIES.get(type))
.map(Supplier::get)
.orElse(null);
}
Since your using integers to identify the type, you could use very easy an array:
private static final Supplier<X>[] FACTORIES = new Supplier[] { A::new, B::new };
public static X createObject(int type) {
return type > 0 && type <= FACTORIES.length ?
FACTORIES[type - 1].get() :
null;
}