I have a requirement where a base class method needs to be sub-classed further. There is a need to register the class object. And depending on configuration, I will decide which class to instantiate.
class Base {
Base () { }
void methodOverride() { // do something; }
}
class Derived1 extends Base {
Derived () {
super();
}
void methodOverride() { // specialize }
}
class Derived2 extends Base {
Derived () {
super();
}
void methodOverride() { // specialize }
}
class SelectorFactory {
Base createClass( int type) {
switch (type) {
case DERIVED_1:
return new Derived1();
case DERIVED_2:
return new Derived2();
case BASE:
return new Base();
}
}
}
public static void main(String[] args){
// read configuration
int type = config.getIntType();
SelectorFactory factory = new SelectorFactory();
someRegisterMethod( factory.createClass() );
}
Question: 1. Do you think there is another way to implement this which would be recommended? Is this the best use of this design pattern?
Aucun commentaire:
Enregistrer un commentaire