I have a super class, with 2 subclasses. If condition foo is true I want one of the subclass to take some action while other subclass should do nothing:
public void doFoo() {
// some 10 lines common to both subclasses.
boolean foo = checkFoo();
/**
* if foo is true, subclass1 should do something. subcass2 does nothing.
*/
// some 10 lines common to both subclasses.
}
The solution I came up with:
public void doFoo() {
// some 10 lines common to both subclasses.
boolean foo = checkFoo();
if (foo) {
doSomething();
}
// some 10 lines common to both subclasses.
}
class subclass1 extends superclass {
public void doSomething() {
// some lines of code.
}
}
class subclass2 extends superclass {
public void doSomething() {
// do nothing
}
}
Any cleaner, better, more standard solution to this common problem?