I have several classes that extend BaseClass. Now I want to define the active class statically in DelegatorContext, and each object creation should be based on an active context.
Example:
class BaseClass {
String firstname, lastname;
}
class FirstClas extends BaseClass {}
class SndClass extends BaseClass {}
class DelegatorContext {
public static BaseClass activeClass;
}
class Delegator {
BaseClass create(String firstname, String lastname) {
return DelegatorContext.activeClass instanceof FirstClass
? new FirstClass(firstname, lastname) : new SndClass(firstname, lastname);
}
}
The example will have an even larger template if additional extension objects are introduced BaseClass.
Is there a better sample for my problem? Maybe even with Java8 and Functions? Or generics?
I use this construct to switch implementation at runtime ...
source
share