Having this tasteful class
public abstract class CakeSkill
{
//..
boolean cherry=false;
private void finalMandatoryTouch()
{
cherry=true;
}
abstract public void cook();
}
A class that extends it would be something like
class Cheff extends CakeSkill
{
//..
void cook()
{
//..Secret recipe
}
}
But of course this won’t work,
finalMandaroryTouch() hasn’t been called, then no cake will end with a cherry..
[EDIT]
This one could be a solution
class MemoriousCheff extends CakeSkill
{
//..
void cook()
{
//..Secret recipe
finalMandatoryTouch();
}
}
but requires :
- Cheff to have a perfect memory that don’t forget to call finalMandatoryTouch()
- Making finalMandatoryTouch() to be protected (at least)
[/EDIT]
It would be great! (but no Java) if something like this could be done
abstract public void cook()
{
@implementedMethod
finalMandatoryTouch();
}
How can be implemented this useful functionality ?
Thank you very much
Change
cookto a protected methodcookImplthen have a public final method called cook:That way the subclass only needs to worry about
cookImpl, but callers ofcookget the cherry on top. Callers not in the same package or class hierarchy won’t even seecookImpl, so won’t be able to call it directly.This is the template method pattern, basically.