?
-
i have a superclass 'A', two subclasses 'A1' and 'A2' and a factory
class 'AFactory'.
there is a method inside 'AFactory' called public static A createA()
which returns an instance of 'A', 'A1' or 'A2' depending on the values
of x and y.
public static A createA(String string) {
// parses string and assigns x and y
...
...
x = ...
y = ...
try {
return new A1(x, y);
} catch (IllegalArgumentException ex) {
}
try {
return new A2(x, y);
} catch (IllegalArgumentException ex) {
}
return new A(x, y);
}
1) is the above try and catch code considered non-ugly for a factory method?
2) i reckon that this method should be placed inside AFactory class
rather than A since the A class should not be aware of how many
subclasses it has.
class 'AFactory'.
there is a method inside 'AFactory' called public static A createA()
which returns an instance of 'A', 'A1' or 'A2' depending on the values
of x and y.
public static A createA(String string) {
// parses string and assigns x and y
...
...
x = ...
y = ...
try {
return new A1(x, y);
} catch (IllegalArgumentException ex) {
}
try {
return new A2(x, y);
} catch (IllegalArgumentException ex) {
}
return new A(x, y);
}
1) is the above try and catch code considered non-ugly for a factory method?
2) i reckon that this method should be placed inside AFactory class
rather than A since the A class should not be aware of how many
subclasses it has.