T&H Metapatterns (non-recursive)
Every developer living in the world of Object-oriented programming knows about OO design patterns. They are useful in solving common but not that trivial problems. Typically, the path starts with GoF pattens. We first learn Factory, Strategy, Builder, then continue with more interesting Decorator and Adapter and with more powerful but maybe not that widely used Command, Chain of Responsibility and so on. Besides GoF pattens there are many more. There are also plenty of patterns outside of OO world.
We know that all the GoF patterns are divided into 3 categories - Creational, Structural and Behavioral. But this division is based on patterns’ intention, not implementation. If you are an experienced developer who has already adopted OO design patterns in real projects you might have noticed that many of them also have implementation commonalities. If you are in the beginning of your path of discovering OO design patterns - you might have even confused different patterns due to similarity in their implementation. These commonalities can be described with meta patterns.
The most interesting and useful meta patterns were proposed by Wolfgang Pree and have a cover name Temple and Hook (T&H) because of their nature. The main idea of templating and hooking should be clear to you if you know, for instance, Template method design pattern. We have a template method with a common algorithm and we have a hook method that is invoked somewhere in the template method. In the Template method design pattern the implementation of the hook method is provided by subclasses. But Pree’s meta patterns go beyond. We abstract away from concrete implementation details like overriding a method and subclassing a class and say that there is some software entity that represents an unchangeable part (our template) and there is another entity (or entities) that is interchangeable (our hook). The classification of T&H meta pattern is presented below.

It looks quite academical but understanding these different meta patterns can be very handy in practice. There is an answer on “Why do I need meta patterns?”.
T&H Non-Recursive Unification
It is the simplest and the most common way to achieve the power of T&H trick. It covers design decisions that combine template and hook in one class. The diagram is presented below.

As you might guess from the diagram, template method design pattern goes under this class because we have template and hook methods defined in one class. A simple example in C# is presented below.
public abstract class FeedGenerator
{
public IEnumerable<IFeedElement> Generate(IFeedElementsProvider provider) {
var feedElements = provider.GetElements();
feedElements = feedElements.Where(fe => fe.IsPublic);
feedElements = Shuffle(feedElements);
return feedElements;
}
protected abstract IEnumerable<IFeedElement> Shuffle(IEnumerable<IFeedElement> feedElements);
}
public class NewsFeedGenerator : FeedGenerator
{
protected override IEnumerable<IFeedElement> Shuffle(IEnumerable<IFeedElement> feedElements) =>
feedElements.OrderBy(fe => Guid.NewGuid()).ToList();
}
As we can see, FeedGenerator class has a method Generate() that returns a complete feed. This general purpose method has all common steps (algorithm) for feed generation, but lets its subclasses to define implementation of shuffling. Subclass NewsFeedGenerator redefines this method1. While this example is given out of any context, there are more things to consider while implementing Template method pattern. You might provide a default implementation for Shuffle() method. You could also make FeedGenerator a concrete class in this case. You might prefer using an interface with default implementation that is available C# version 8. It is very easy to start going deep into implementation details and some of them can really wake up your geeky nature. While designing a solution it is better to think about T&H Non-Recursive Unification meta pattern as a general approach. Complex name does not help here but it’s not crucial to remember it. The main idea demonstrated with a simple diagram above is much more important.
Common template and variating hook in the same class is everything this meta pattern is about. Besides Template method pattern, Factory method also falls under this category while in both patterns template and hook are declared in the same class3. Their diagrams are presented below.


T&H Non-Recursive 1:1 Connection
This meta pattern is even simpler than Unification. It simply points at the use of delegation for interchanging some functionality in a template method. The diagram is presented below.

The implementation of hook is provided by delegate and template class has a reference to the delegate. Strategy design pattern perfectly fits the description. A simple example is provided below.
public class FeedGenerator
{
private IShuffler _shuffler;
public FeedGenerator(IShuffler shuffler)
{
_shuffler = shuffler;
}
public IEnumerable<IFeedElement> Generate(IFeedElementsProvider provider)
{
var feedElements = provider.GetElements();
feedElements = feedElements.Where(fe => fe.IsPublic);
feedElements = _shuffler.Shuffle(feedElements);
return feedElements;
}
}
We can solve the same problem of feed generation with hooked shuffling implementation using T&H Non-Recursive 1:1 Connection meta pattern. Strategy allows us to change parts of implementation in FeedGenerator without inheritance relation. In this example it even looks more reasonable to use simple delegation. Because we depend on interface accepting a concrete instance via constructor and not instantiating a shuffler object in the template class - we provide a seam2 to clients, not only to subclasses. It lets clients of the FeedGenerator to choose an implementation of Shuffle() method by passing different instances that implement IShuffler. Again, there are plenty of variation of how exactly this will be implemented and during design you better think of the general meta pattern keeping the idea and diagram in mind.
Besides Strategy design pattern, Bridge, Builder, Mediator, State and Visitor fall under this meta pattern3. Diagrams of Strategy and Mediator are presented below.


T&H Non-Recursive 1:N Connection
This meta pattern is a slight modification of T&H Non-Recursive 1:1 Connection where some class references several instances of hook class. The diagram of the meta pattern is shown below.

The most obvious example of the pattern is Observer. Canonical implementation is presented below.
public class FeedGenerator
{
private List<IObserver> _observers = new List<IObserver>();
public void RegisterObserver(IObserver observer)
{
_observers.Add(observer);
}
public IEnumerable<IFeedElement> Generate(IFeedElementsProvider provider)
{
var feedElements = provider.GetElements();
feedElements = feedElements.Where(fe => fe.IsPublic);
foreach (var observer in _observers)
{
observer.Notify();
}
return feedElements;
}
}
public interface IObserver
{
void Notify();
}
In the example the same FeedGenerator is our subject of interest. Its concrete instances will be potentially observed by instances of classes implementing IObserver interface. For the sake of example, observers can be notifiers that will notify users via web and mobile applications about newly generated feed. To achieve the required behavior and have possibility to add more observers when more user applications arise - we need to store a collection of observers in FeedGenerator. We also have to provide a possibility to register (and often unregister) observers in client code. Then we notify all observers iterating over all of them by calling Notify() method.
There are many different implementations of Observer design pattern. There is even language support in C# - events.
Besides the Observer, there are Abstract Factory, Adapter, Command, Flyweight, Iterator, Prototype, Proxy and Visitor that fall under T&H Non-Recursive 1:N Connection meta pattern3.
Diagrams of Observer and Iterator are presented below.


To be continued
The next article presents recursive T&H meta patterns.
References
- We assume that this implementation is good enough to shuffle news posts. For better versions of Shuffle method look at SO answers here or here
- Question on SO - Problem with understanding “seam” word
- Hayashi, Shinpei & Katada, Junya & Sakamoto, Ryota & Kobayashi, Takashi & Saeki, Motoshi. (2008). Design Pattern Detection by Using Meta Patterns. IEICE Transactions. 91-D. 933-944. 10.1093/ietisy/e91-d.4.933. Researchgate