I am implementing template pattern in Java. Let us assume the following piece of code:
public abstract class A {
public final void work() {
doPrepare();
doWork();
}
protected abstract void doPrepare();
protected abstract void doWork();
}
public final class B extends A {
@Override
protected abstract void doPrepare() { /* do stuff here */ }
@Override
protected abstract void doWork() { /* do stuff here */ }
}
public final class Main {
public static void main(String[] args) {
B b = new B();
b.work();
}
}
I consider it a problem that one is easily able to accidentally call b.doWork()
instead of always calling b.work()
. What is the most elegant solution, if possible, to "hide" the hooks?
Aucun commentaire:
Enregistrer un commentaire