- The forte of Builder is constructing a complex object step by step.
- An abstract base class declares the standard construction process, and concrete derived classes define the appropriate implementation for each step of the process.
- Builder is a class (or set of classes) responsible for constructing an object. Each builder constructs a different part of the object.
#include <string>
#include <iostream>
class Message
{
public:
std::string greeting, recipient;
void send()
{
std::cout << greeting << " " << recipient << "!" << std::endl;
}
};
class Builder
{
public:
virtual ~Builder() { }
virtual void build(Message & msg) const=0;
};
class Hello : public Builder
{
public:
void build(Message & msg) const
{
msg.greeting = "Hello";
}
};
class World : public Builder
{
public:
void build(Message & msg) const
{
msg.recipient = "world";
}
};
void hello_world(const Builder & stage1, const Builder & stage2)
{
Message msg;
stage1.build(msg);
stage2.build(msg);
msg.send();
}
int main()
{
hello_world(Hello(), World());
return 0;
}