void buildSeat();
void buildDerailleur();
void buildHandlebar();
void buildSprocket();
void buildRack();
void buildShock();
std::string getBikeName() const {
return "RacingBike";
}
};
class BicycleTechnician {
public:
BicycleTechnician() {
builder = 0;
}
void setBuilder(BicycleBuilder* b) {
builder = b;
}
void construct();
private:
BicycleBuilder* builder;
};
#endif ///:~
A Bicycle holds a vector of pointers to BicyclePart representing the parts used to construct the bicycle. To initiate the construction of a bicycle, a technician calls BicycleBuilder::createproduct( ) on a derived BicycleBuilder object. The BicycleTechnician::construct( ) function calls all the functions in the BicycleBuilder interface (since it doesn’t know what type of concrete builder it has). The concrete builder classes omit (via empty function bodies) those actions that do not apply to the type of bicycle they build, as you can see in the following implementation file.
//: C10:Bicycle.cpp {O}
// Defines classes to build bicycles
// Illustrates the Builder Design Pattern
#include <cassert>
#include <cstddef>
#include <iostream>
#include "Bicycle.h"
#include "../purge.h"
using namespace std;
// BicyclePart implementation
BicyclePart::BicyclePart(BPart bp) {
id = bp;
}
ostream&
operator<<(ostream& os, const BicyclePart& bp) {
return os << bp.names[bp.id];
}
std::string BicyclePart::names[NPARTS] = {
"Frame", "Wheel", "Seat", "Derailleur",
"Handlebar", "Sprocket", "Rack", "Shock"};
// Bicycle implementation
Bicycle::~Bicycle() {
purge(parts);
}
void Bicycle::addPart(BicyclePart* bp) {
parts.push_back(bp);
}
ostream&
operator<<(ostream& os, const Bicycle& b) {
os << "{ ";
for (size_t i = 0; i < b.parts.size(); ++i)
os << *b.parts[i] << ' ';
return os << '}';
}
// MountainBikeBuilder implementation
void MountainBikeBuilder::buildFrame() {
product->addPart(new BicyclePart(BicyclePart::FRAME));
}
void MountainBikeBuilder::buildWheel() {
product->addPart(new BicyclePart(BicyclePart::WHEEL));
}
void MountainBikeBuilder::buildSeat() {
product->addPart(new BicyclePart(BicyclePart::SEAT));
}
void MountainBikeBuilder::buildDerailleur() {
product->addPart(
new BicyclePart(BicyclePart::DERAILLEUR));
}
void MountainBikeBuilder::buildHandlebar() {
product->addPart(
new BicyclePart(BicyclePart::HANDLEBAR));
}
void MountainBikeBuilder::buildSprocket() {
product->addPart(new BicyclePart(BicyclePart::SPROCKET));
}
void MountainBikeBuilder::buildRack() {}
void MountainBikeBuilder::buildShock() {
product->addPart(new BicyclePart(BicyclePart::SHOCK));
}
// TouringBikeBuilder implementation
void TouringBikeBuilder::buildFrame() {
product->addPart(new BicyclePart(BicyclePart::FRAME));
}
void TouringBikeBuilder::buildWheel() {
product->addPart(new BicyclePart(BicyclePart::WHEEL));
}
void TouringBikeBuilder::buildSeat() {
product->addPart(new BicyclePart(BicyclePart::SEAT));
}
void TouringBikeBuilder::buildDerailleur() {
product->addPart(new BicyclePart(BicyclePart::DERAILLEUR));
}
void TouringBikeBuilder::buildHandlebar() {
product->addPart(
new BicyclePart(BicyclePart::HANDLEBAR));
}
void TouringBikeBuilder::buildSprocket() {
product->addPart(new BicyclePart(BicyclePart::SPROCKET));
}
void TouringBikeBuilder::buildRack() {
product->addPart(new BicyclePart(BicyclePart::RACK));
}
void TouringBikeBuilder::buildShock() {}
// RacingBikeBuilder implementation
void RacingBikeBuilder::buildFrame() {
product->addPart(new BicyclePart(BicyclePart::FRAME));
}
void RacingBikeBuilder::buildWheel() {
product->addPart(new BicyclePart(BicyclePart::WHEEL));
}
void RacingBikeBuilder::buildSeat() {
product->addPart(new BicyclePart(BicyclePart::SEAT));
}
void RacingBikeBuilder::buildDerailleur() {}
void RacingBikeBuilder::buildHandlebar() {
product->addPart(
new BicyclePart(BicyclePart::HANDLEBAR));
}
void RacingBikeBuilder::buildSprocket() {
product->addPart(new BicyclePart(BicyclePart::SPROCKET));
}
void RacingBikeBuilder::buildRack() {}
void RacingBikeBuilder::buildShock() {}
// BicycleTechnician implementation
void BicycleTechnician::construct()
{
assert(builder);
builder->createProduct();
builder->buildFrame();
builder->buildWheel();
builder->buildSeat();
builder->buildDerailleur();
builder->buildHandlebar();
builder->buildSprocket();
builder->buildRack();
builder->buildShock();
}; ///:~
The Bicycle stream inserter calls the corresponding inserter for each BicyclePart, and that prints its type name so that you can see what a Bicycle contains. The power of this pattern is that it separates the algorithm for assembling parts into a complete product from the parts themselves and allows different algorithms for different products via different implementations of a common interface. Here is a sample program, along with the resulting output, that uses these classes.
//: C10:BuildBicycles.cpp
//{L} Bicycle
#include <cstddef>
#include <iostream>
#include <map>
#include <vector>
#include "../purge.h"
#include "Bicycle.h"
using namespace std;
// Constructs a bike via a concrete builder
Bicycle*
buildMeABike(BicycleTechnician& t, BicycleBuilder* builder) {
t.setBuilder(builder);
t.construct();
Bicycle* b = builder->getProduct();
cout << "Built a " << builder->getBikeName() << endl;
return b;
}
int main() {
// Create an order for some bicycles
map <string, size_t> order;
order["mountain"] = 2;
order["touring"] = 1;
order["racing"] = 3;