I'm trying to improve this document parser and there is one main loop that goes over all of the main elements (let's call them Nodes). Each step (Step) can either Consume or Ignore the Node that it is passed. A Step can also be greedy and consume more than the single Node than was passed to it. A Step could restrict its transition to other states like a state machine. I can't remove Node from the list of nodes as that would get me into collection modified while enumerating problems and it would be difficult to accelerate the main loop

What design would work well for this?

Here is something I have been considering to implement.

class Node { ... }
class Step 
    void Handle(NodeContext);
class NodeContext
     Node Node { get; }
     void Ignore();
     void Consume();
  • 1
    Possible duplicate of Choosing the right Design Pattern – gnat Jun 16 '17 at 19:20
  • I don't think this question is about design patterns, so it should be edited to say "design" instead of "design pattern", instead of being closed as a duplicate of the Big Design Pattern Question. I've submitted a proposed edit. – Tanner Swett Jun 16 '17 at 19:58
  • I would not worry about the class structure as much as the algorithm. In general, you should never modify the list you are iterating, unless you are working from the back to the front, and only delete the last element. – Frank Hileman Jun 16 '17 at 21:51
  • @gnat: I recommend to reread my answer here and go through the steps I listed there: softwareengineering.meta.stackexchange.com/a/8562/9113 – Doc Brown Jun 22 '17 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.