0

For context on this question; we have the following data structure constructed (example)

Book
  - Table of Contents
  - Chapter
      - Chapter Title
      - Pages
          - Paragraphs
          - Page Number

This structure is constructed using the Composite pattern; we also have data (textual metadata and media info) in database tables which gets assigned to each object in the tree by visitors.

The idea is now that I want to validate the structure; but this has to be done from the leaf nodes up;

i.e. Verify that a page has a paragraph and page number to validate a page; and if so then chapter can be validated and so on.

I'm in a bind because the Visitor pattern abstracts the algorithm from the data structure; therefore it's not possible to add return values directly into the method I'm using to traverse the tree; and would be pointless to add another method to do similar job; here is the method on the CompositeObject which allows the visitors to traverse the tree:

/**
 * Accept Visitor
 * @param  ObjectTree_Visitor_Interface $visitor
 * @return void
 */
public function acceptVisitor(ObjectTree_Visitor_Interface $visitor) {
    $visitor->visit($this); 
    if(count($this->objects) > 0) {
        foreach($this->objects as $object) {
            $object->acceptVisitor($visitor);
        }
    }
}

Any ideas on alternative methods that I could use to traverse this structure?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
csdev86
  • 83
  • 1
  • 1
  • 5
  • Little confused. The issue the composite pattern? To say you don't distinguish between nodes and therefor don't have concrete useful methods to validate parent nodes? Blinded by patterns... Sometimes you just need to make it work, then figure out what to call it. – ficuscr Jun 25 '13 at 17:57
  • We needed the composite pattern as we're doing other operations on the object structure using visitors (e.g. assigning metadata, attaching media etc). It also makes it easier to save each object later using Doctrine. What I'm looking for is a way to validate each component from the leaf nodes upwards in turn – csdev86 Jun 25 '13 at 18:19

0 Answers0