Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilder design pattern: why do we need a Director?
    primarykey
    data
    text
    <p>Recently I've come across the Builder design pattern. It seems that different authors use "Builder pattern" to refer to different flavours, so let me describe the pattern I'm asking about.</p> <p>We have an algorithm for creating <em>products</em>, i.e., objects of different types. At a sufficiently high level of abstraction the algorithm is the same for all product types, but each product type requires a different implementation of each of the algorithm's abstract steps. For example, we might have the following cake-baking algorithm:</p> <pre><code> 1. Add liquids. 2. Mix well. 3. Add dry ingredients. 4. Mix well. 5. Pour batter into baking pan. 6. Bake. 7. Return baked cake. </code></pre> <p>Different cakes would require different implementations of these steps, i.e., what liquids/dry ingredients to use, what speed to mix at, how long to bake, etc.</p> <p>The pattern says to do it like so. For each product we create a <em>concrete builder</em> class with an implementation for each of the above steps. All of these classes are derived from an <em>abstract builder</em> base class, which is essentially an interface. So, for example, we will have an abstract base class <code>CakeBaker</code> with pure virtual methods <code>AddLiquid()</code>, <code>MixLiquids()</code>, etc. The concrete cake bakers will be concrete subclasses, e.g.,</p> <pre><code>class ChocolateCakeBaker : public CakeBaker { public: virtual void AddLiquids() { // Add three eggs and 1 cup of cream } virtual void AddDryIngredients() { // Add 2 cups flour, 1 cup sugar, 3 tbsp cocoa powder, // 2 bars ground chocolate, 2 tsp baking powder } ... ... }; </code></pre> <p>The <code>LemonCitrusCakeBaker</code> would also be a subclass of <code>CakeBaker</code>, but would use different ingredients and quantities in its methods.</p> <p>The different cake types will similarly be subclasses of an abstract <code>Cake</code> base class.</p> <p>Finally, we have a class to implement the abstract algorithm. This is the <em>director</em>. In the bakery example we might call it <code>ExecutiveBaker</code>. This class would accept (from the client) a concrete builder object and use its methods in order to create and return the desired product.</p> <p>Here's my question. Why do we need the director to be separate from the abstract builder? Why not roll them into a single builder abstract base class, making the original abstract builder's public methods protected (and the concrete subclasses override these as before).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload