in reply to OO Parser Class with Callbacks - Confusion

It looks to me like Frontend has-a Backend, not Frontend is-a Backend. Can you describe further why you think having shared reused code and protocol was useful in your design?
  • Comment on Re: OO Parser Class with Callbacks - Confusion

Replies are listed 'Best First'.
Re^2: OO Parser Class with Callbacks - Confusion
by Anonymous Monk on Dec 04, 2006 at 03:45 UTC

    You know, I think you might have nailed it. Intuitively that seems correct.

    I did it this way to divide the parsing of the input file from the format of the output. So the backend would say "I've found a glark element", and frontend A may say "Ignore it", while frontend B may say "Concatenate it with the preceding output".

    The plan was for the client to say:

    my $obj = Backend->new(frontend=>FrontendClass);

    But, as you point out, far more sensible is for them to say:

    my $obj = Frontend->new;

    Then to have Frontend use Backend. :-) Thank you very much!

      Yep, that works perfectly. I had a feeling it was a fundamental mistake. :-)

      I now use Frontend directly like this:

      my $obj = Frontend->new; $obj->parse($file);

      Frontend inherits from Backend, and Frontend doesn't provide a _parse_ method, so it dispatches it up the chain to Backend. Backend does have this method and starts parsing the file. When it hits a token it calls

      $self->handle(name=>'token_name',args=>['arg1','arg2'])
      which is a generic dispatch mechanism in Backend.

      The code for _handle_ is simplified now because it does something like:

      if ($self->can($token)) { return $self->$token($args); }

      IOW, it looks for a method with the same name as the token in the current class and when it fails it passes it to Frontend which does implement said method.:-)