I have the following code in PHP:
abstract class AlgorithmOnArray {
public function runOn($array) {
$this->setup();
foreach ($array as $item) {
$this->processItem();
}
$this->cleanup();
}
public abstract function processItem();
public function cleanup() { }
public function setup() { }
}
My question is regarding this notion I have here of having empty but implemented methods for cleanup
and setup
. The idea here is that while normally I'd have them as abstract, I also want classes which extend the AlgorithmOnArray
class to be able to fallback to a default implementation.
The question here is whether this is deemed a reasonable thing to do or should I strictly say that these methods also need to be abstract to force child algorithms to implement them?
cleanup()
in the derived class, and callrunOn()
from the derived class, won'trunOn()
still call the cleanup function here, where it has no implementation?