I have a few operations to make over many similar elements. I would like to collect data from each element firstly and next bind all the data to an object (binding is expensive operation so I need to do it once).
Is it consistent with the Visitor pattern?
Example of my problem:
class Element {
public $name;
public function accept(VisitorInterface $visitor) {
$visitor->visitElement($this);
}
}
class SimpleVisitor implements VisitorInterface {
private $data = [];
public function visitElement(Element $element) {
$this->data[] = $element->name;
}
public function bindData(Object $object) {
$object->setNames($this->data);
}
}
$visitor = new SimpleVisitor();
$object = new Object();
$elementA = new Element();
$elementA->name = 'test1';
$elementA->accept($visitor);
$elementB = new Element();
$elementB->name = 'test2';
$elementB->accept($visitor);
$visitor->bindData($object);