1

I am taking this basic course in design patterns that explains "Template-And-Hook Patterns" as an introduction by starting with the "Template Method Pattern" then goes a little bit complex into "Template Class Patterns" and finally the "Generic Template Class Patterns".

What still unclear to me is what the "Generic Template Class" really adds to this ?

Here is how it was explained:

enter image description here

Where do we exactly see the static generic expansion here ?

Ahmed Samy
  • 1,006
  • 2
  • 11
  • 24
  • 2
    The advantage that type parameters usually have over direct inheritance is that you know the types at compile time. When you instantiate a TemplateClass you have to specify the type parameter T, that's how you get type safety. The alternative would be that the TemplateClass accepts a HookClass implementation as a dependency or something, and you can't know the concrete type until runtime – SrThompson Dec 29 '19 at 22:30
  • There is also a small representation mismatch in your diagram. In ConcreteTemplateHookClassA and ConcreteTemplateHookClassB, the type of field hookObject should be respectively ```ConcreteHookValueA```and ```ConcreteHookValueB``` instead of A and B. This is what matches the generic type replacement provided at the declaration of the concrete subclasses – alainlompo Dec 30 '19 at 01:16
  • This looks like an anti-pattern to me: it appears to violate the Dependency Inversion Principle. A dependency on the concrete type of `HookClass` indicates something wrong in the type hierarchy. – jaco0646 Dec 30 '19 at 22:31

0 Answers0