1

Design Patterns, by Gamma et al, describes Builder pattern:

Intent

Separate the construction of a complex object from its representation so that the same construction process can create different representations

Motivation

enter image description here

Structure

enter image description here

Implementation

Why no abstract class for products? In the common case, the products produced by the concrete builders differ so greatly in their representation that there is little to gain from giving different products a common parent class. In the RTF example, the ASCIIText and the TextWidget objects are unlikely to have a common interface, nor do they need one. Because the client usually configures the director with the proper concrete builder, the client is in a position to know which concrete subclass of Builder is in use and can handle its products accordingly.

Is there some condition on what kinds of Products (e.g. ASCIItext, TexText, and TextWidget in Motivation example) can share the same Builder base class and the same Director, since they don't share a common parent class?

1 Answer 1

1

Your question makes sense but in fact I think that defining or not a base class for the Product class depends really on the way which you want to implement your builders.

I think that default implementations of the abstract builder class may be aware on a base class for the Product to build but really no obligation.

For example in the quoted example, it may just append the String returned by the method to a String that is the state of the object to build. In this case, why defining an abstract Product class ? We don't need it.


Even in a more complex built object for any methods implemented in the abstract builder class, you could store each part of the object to build in the state of the abstract builder class and use these parts without the need of any base class for the Product class.

For example here is an implementation without base class for the Product

Client :

Builder builder = new ParadiseWorldBuilder();
World world = director.build(builder);

Director :

World build(){    
    builder.buildLands(...); // defined in abstract class for example
    builder.buildPeople(...);
    builder.buildAnimals(...);
    return builder.getWorld();
 }

Abstract builder class :

private List<Land> lands = ...;
List<Land> getLands(){return lands;}

void buildLands(){
    for(....){ 
        lands.add(...);
    }
}

Concrete builder class :

World getWorld(){
  return new ParadiseWorld(getLands(), ...); // use the state of the abstract class
}

Now just to show you that it is a question of choice, I will change the implementation of the example and rely on a common base class for the Product class (here World).

Implementation with base class for the Product

Client :

Builder builder = new ParadiseWorldBuilder();
World world = director.build(builder);

Director :

World build(){    
    builder.buildLands(...); // defined in abstract class for example
    builder.buildPeople(...);
    builder.buildAnimals(...);
    return  builder.getWorld();
 }

Abstract builder class :

protected World world = createEmptyWorld(); // abstract method
void buildLands(){
    for(....){
        world.addLands(...); 
    }
}
World getWorld(){
  return world;
}

Abstract Product class :

public World{
   void addLands(...){
        // ...
   }
}

Product class :

public ParadiseWorld extends World{
     //...
}

Concrete builder class :

World createEmptyWorld(){
   return new ParadiseWorld();
}

IHMO in this example the approach without base class for Product is much better. It reduces the coupling between the Builders and the Products. Besides, here the hierarchy appears superficial and the delegation between the builder and the product to create for each built part looks an overhead and prevent immutability for the Product class(that may be desirable).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.