I have written a code which applies the "Builder" design pattern but some errors appeared:
The code:
class Builder {
public:
int m_suger;
int m_cup;
string m_flavour;
Builder* sugar(int sugar) {
this->m_suger = sugar;
return this;
}
Builder* cup(int cup) {
this->m_cup = cup;
return this;
}
Builder* flavour(string flavour) {
this->m_flavour = flavour;
return this;
}
Tea* build() {
return new Tea(this);
}
};
class Tea {
public:
int m_suger;
int m_cup;
string m_flavour;
Tea(Builder* b) {
m_suger = b->m_suger;
m_cup = b->m_cup;
m_flavour = b->m_flavour;
cout << "Hot " << b->m_cup << " cup of tea is comming!, with " << b->m_flavour << endl;
}
};
int main(int argc, char *argv[]) {
Tea* mintTea = new Builder()->cup(2)->sugar(3)->flavour("mint")->build();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire