Template pattern provides algorithm in the base class whose steps can be modified in the derived class. In Builder pattern, concrete builder exposes methods for building a product which are called from the Director class.
I understand there is difference with respect to the purpose of using these patterns. Template pattern is a behavioral pattern which changes one or more steps in a template whereas builder pattern is a Creational pattern.
Apart from the above said difference, are there any other differences?
Isn't director in builder pattern acting as a base template in template pattern. The concrete builders are acting like derived classes in the template pattern with substitutable steps?
Can someone please clarify. Thank you.
I am referring http://www.dofactory.com/Patterns/Patterns.aspx
The difference between the two is that while the strategy pattern allows different implementations to use completely different ways of the achieving the desired outcome, the template method pattern specifies an overarching algorithm (the "template" method) which is be used to achieve the result -- the only choice left ...
But, the key difference is that Strategy Pattern is about modifying a behaviour of a context in runtime using strategies, while Template Method Pattern is about following a skeleton implementation of an algorithm and modifying its behaviour by overriding methods of the skeleton class in the subclasses.
A template is a master pattern (in word processing a standard sequence of words, phrases or sentences) which is copied by the user, who can normally add something (e.g. someone's name) to it. A pattern (as used in social security numbers) is a sequence of data that follows a rule.
From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
Template method is really just a way to define some methods that each subclass must define.
The builder pattern is used to construct a more complex object.
Lets say that we want to construct different Saab (car brand) models. Each model has different engines, lights etc.
If we were to use the template method pattern we had to create one class for each single combination of cars or use some nasty inheritance hierarchies. A lot of those methods would also contain duplicate code.
With the build pattern we can instead take different parts and compose those together to a complete car. Hence we can reuse a engine for every model if needed and we can also customize each part of the car.
I found the same question very interesting.
The Saab car example is interesting but it does not adhere to the description of Builder pattern in "Gang of Four" (Design Patterns).
I will be using "Gang of Four" terminology.
In Gang of Four, there cannot be a blend of concrete builders per each invocation of aDirector->Construct()
so the Saab example, while is interesting does not answer the question really for me.
I see some:
The separation of the director object from the builder hierarchy is a key difference. In the Template Factory both the method that embodies the generalized flow and the overriden methods are members of the same class. This makes the Builder pattern better at encapsulating the internal stages of the process since client code is less likely to come in contact with such methods if accessing only the Director object. Builder pattern also allows for a formulation of the assembly process completely independently from the Builder hierarchy, allowing for a more flexible replacement of the builder instance when needed. For example, once you have a director instance at hand you can easily build several representations of the product, each time dynamically replacing the concrete builder. So the builder is more dynamic and better encapsulates the inner workings of the concrete builder.
In addition, if you want to elaborate on the Director object by inheritance you may do so without multiplying your hierarchy. For example, you may have an express process of building for saving time before the final construction - you can subclass the Director object or even use "Template Method" on it in itself to customize it by inheritance without requiring to reimplement your concrete builders.
But this leads us to consider another pattern which is closely related to "Template Factory" - the "Strategy" pattern.
Strategy is very much like Template Factory, with two clear differences: it too separates a Context object from the Strategy hierarchy allowing for switching algorithms for a single problem instance in runtime. Another difference is that the examples seem to suggest that invoking the Strategy does not necessarily involve a complex or structured process as in "Template Method".
But I bring it here for as an analogue to Builder to get to another point - If "Strategy" is parallel in its class structure to Builder, then the parallel creational pattern to "Template Method" should be "Factory Method". This is clear, not only by name, but interestingly enough, the discussions both chapters of the book for "Factory method" and "Template method" use almost identical examples (an application for editing documents).
So, without going into the question of what is the difference between creational and behavioral patterns, I tend to think that both Builder and Factory Method are basically specific cases and refinements of Strategy and Template Method, respectively.
So the question becomes - if you see no difference between Builder and Template Factory - try to answer these questions:
What perspective do you prefer having on that particular part of the system? Is it "behavior" or "creation"? and
Do you require strong encapsulation, or the dynamic replacement or deployment or tweaking of a builder instance, on one hand, or do you expect complexity (by inheritance, composition, or otherwise) to evolve around the creation process or template method? If the answer to any of these questions go with the Builder/Strategy structure. Otherwise, the use simple polymorphysm of relation or behavior in the XX Method patterns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With