I am trying to create a sprite parent class that uses some of it methods to filter out some of the dud conditions by returning null prior to moving onto the child method. I think this is referred to as a 'bouncer pattern' when used in a single method but couldn't find any examples that extend across parent / child objects.
Below is an basic example of the code;
class parent ()
{
public void update()
{
if (this.count<0)
{
return;
}
// do parent stuff
}
}
class child extends parent ()
{
public void update()
{
super.update()
//do child stuff here
}
}
When i run the child update method i would like it to run the parent method and if the count is less than 0 just stop an not bother with the parent and child stuff.
What i am finding is that once the count is less than 0 the parent stuff stops but the child stuff keeps going. It is like both of the update methods are being treated as separate functions.
I believe i need a way to append the child update method to the child update method. I am hoping there is a simple way of achieving this but will accept complicated solutions as long as i don't have to write the bouncer/filter code in every child class.
Any help is much appreciated.
Jason
#Alternative solution
Proposed code class parent () { public void update() { if (this.count<0) { return; } // do parent stuff child_update()
}
child_update()
{
//place hold to be overridden by child
}
}
class child extends parent ()
{
@Override
private void child_update()
{
//do child stuff here
}
}
I think this solves my problem but haven't had chance to test it yet. When i call the child object update method it should be directed to the parent method, which performs the bouncer/validation checks and if applicable runs the child update that has been overridden by the child method.The child method would be set to private so it can't be called without the validation checks.
I personally think this is a cleaner solution than the boolean solution being proposed but would like to know what other people think.
thanks
Jason