Jump to content

Archived

This topic is now archived and is closed to further replies.

Rike

Template method pattern and process interruption

Recommended Posts

As written in title, I use Template method pattern. Template method calls other overloaded methods with particular implementation, but how to solve situation, when this method need to break whole algorithm?

Yes, throw exception, that should be the way, but not every interruption is really exception. Is this a mistake in design, or there is any way to implement interruption of template method?

Share this post


Link to post
Share on other sites

Hallo, it's not easy to write short code, because full version of Controller is very complex (DI), but I try:

abstract class Controller {

  // Template method
  public final function control(...) {
    try {
      $this->before();
      $this->process();
      return $this->after();
    } catch ...

  }

  protected function before(...) {
    // here is code to execute before processing of controller
    // should throw many exceptions (rights etc.)
   }


  protected function process(...) { 
    // body of controller 
  }

  protected function after(...) {
    // typically return views data...
  }

}

Method Controller::before() throws exceptions on a number of counts, but I am looking for clean way how to throw "silent exception", because controller should be called as "service" and in this case I do not need error message (you are not logged in...) but only stop processing and return context to other controller in queue.

 

Yes, it's easy to define some SilentException extends \Exception, but this I feel a little bit dirty.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.