|
-
Jul 11th, 2015, 12:37 PM
#1
Thread Starter
Member
Is Template Method design pattern and Hook the same thing by different names?
Colleagues,
Is Template Method design pattern and Hook the same thing by different names?
Odd thing is, when I look up hook design pattern on Google, 90% of search results go to pages that describe the Template Method design pattern. I'm trying to find the historical explanation for this. Is Hook a non-OO spiritual precursor to Template Method?
What had prompted me to look into the Hook pattern was this Miro Samek's app note about pattern which he calls "Ultimate Hook". This app note is about message handling by hierarchical state machines. Samek didn't invent the term "Ultimate Hook", though. It was mentioned in 1995 by Petzold in his book on Win 95 programming. The context was plain C. WinMain(...) and WndProc(...) without classes.
Any suggestion, insight or reference is really appreciated!
Cheers,
- Nick
-
Aug 9th, 2015, 01:19 AM
#2
Thread Starter
Member
Re: Is Template Method design pattern and Hook the same thing by different names?
-
Aug 14th, 2015, 04:29 PM
#3
Re: Is Template Method design pattern and Hook the same thing by different names?
No, I don't think they're the same thing, though you could use the same mechanisms as template method to implement some hooking patterns.
Template method concerns itself with ensuring a process always completes the same way, and dictates a contract of abstract methods that are part of the process. Sorting an array's a good example, so long as you give it a comparison method that tells it which of two values are greater it can sort any array. It doesn't make sense to not implement the helper methods in Template Method, and you never override the template method itself.
Ultimate Hook concerns itself with ensuring there is a default response for a universe of expected inputs but a mechanism for customizing the response to a subset of those inputs. WndProc() does seem to be an example, because you handle the messages you are interested in but defer to DefWndProc() if you don't want to handle the current message. (It's interesting to note this uses function pointers to achieve a kind of polymorphism in a language with no OO facilities.) In an OO Ultimate Hook implementation, you'd provide an Overridable method with the default implementation for all messages, then in your derived classes override that method, delegating to the base for inputs you don't want to augment. Ultimate Hook doesn't specify helper methods, and you are expected to override the base method *and* delegate to its implementation.
So they're different solutions to different problems. You could certainly have Ultimate Hook define an Overridable default method for every case, but that wouldn't really make it Template Method because TM is an abstract definition of a process and has no sensible default.
I guess in real world analogies:
- Template Method is for describing to a person how to complete a transaction at a grocery store. No matter what kind of baskets or carts they have, no matter if it's a self checkout, no matter someone else bags the groceries, the process "Approach register, scan all items, pay, bag, leave" is the same.
- Ultimate Hook is how the register at the store figures the sum of all items when coupons and deals like "buy 2 get 1 free" are in effect. Most items are just "add price to sum", but for some items a more complicated price calculation occurs, like "This is the 2nd coconut, it is full price." then "This is the 3rd coconut, it is free."
You haven't found a formal pattern called "hook" because "hooking" is just a term for the concept of providing an interface for users to give a system code to execute when something happens. Hooks are tools to implement patterns, and often just function pointers.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Aug 14th, 2015, 04:30 PM
#4
Re: Is Template Method design pattern and Hook the same thing by different names?
No, I don't think they're the same thing, though you could use the same mechanisms as template method to implement some hooking patterns.
Template method concerns itself with ensuring a process always completes the same way, and dictates a contract of abstract methods that are part of the process. Sorting an array's a good example, so long as you give it a comparison method that tells it which of two values are greater it can sort any array. It doesn't make sense to not implement the helper methods in Template Method, and you never override the template method itself.
Ultimate Hook concerns itself with ensuring there is a default response for a universe of expected inputs but a mechanism for customizing the response to a subset of those inputs. WndProc() does seem to be an example, because you handle the messages you are interested in but defer to DefWndProc() if you don't want to handle the current message. (It's interesting to note this uses function pointers to achieve a kind of polymorphism in a language with no OO facilities.) In an OO Ultimate Hook implementation, you'd provide an Overridable method with the default implementation for all messages, then in your derived classes override that method, delegating to the base for inputs you don't want to augment. Ultimate Hook doesn't specify helper methods, and you are expected to override the base method *and* delegate to its implementation.
So they're different solutions to different problems. You could certainly have Ultimate Hook define an Overridable default method for every case, but that wouldn't really make it Template Method because TM is an abstract definition of a process and has no sensible default.
I guess in real world analogies:
- Template Method is for describing to a person how to complete a transaction at a grocery store. No matter what kind of baskets or carts they have, no matter if it's a self checkout, no matter someone else bags the groceries, the process "Approach register, scan all items, pay, bag, leave" is the same.
- Ultimate Hook is how the register at the store figures the sum of all items when coupons and deals like "buy 2 get 1 free" are in effect. Most items are just "add price to sum", but for some items a more complicated price calculation occurs, like "This is the 2nd coconut, it is full price." then "This is the 3rd coconut, it is free."
You haven't found a formal pattern called "hook" because "hooking" is just a term for the concept of providing an interface for users to give a system code to execute when something happens. Hooks are tools to implement patterns, and often just function pointers. It's a little like asking for information about an "addition" pattern.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|