Jump to content

Template method pattern and process interruption


Rike

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?

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.