Rike Posted August 5, 2014 Share Posted August 5, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/290290-template-method-pattern-and-process-interruption/ Share on other sites More sharing options...
ignace Posted August 5, 2014 Share Posted August 5, 2014 Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/290290-template-method-pattern-and-process-interruption/#findComment-1486920 Share on other sites More sharing options...
Rike Posted August 6, 2014 Author Share Posted August 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290290-template-method-pattern-and-process-interruption/#findComment-1486967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.