Factory Pattern
Example
Concrete Creators: Taco Bell, Del Taco, and Taco John’s
Taco header (taco.h)
#ifndef TACO_H_
#define TACO_H_
#include <string>
#include <vector>
class Taco {
private:
std::string t_name;
std::string t_shell;
std::string t_meat; // Meat
std::vector<std::string> t_toppings; // Toppings that go ontop of our tacos
public:
Taco();
virtual ~Taco();
std::string GetName()
{
return t_name;
}
void Cook(); // Cook Taco
void Prepare(); // Prepare the Taco
void Wrap();
};
#endif
Taco File (taco.cpp)
#include "taco.h"
#include <iostream>
Taco::Taco() {}
Taco::~Taco(){}
void Taco::Grill()
{
std::cout << "Grilling the Taco.....done " << std::endl;
}
void Taco::Wrap()
{
std::cout << "Wrapping your " << getName() << std::endl;
}
void Taco::Prepare {
// Preparing the taco
std::cout << "Preparing " << t_name << std::endl;
std::cout << "Adding the " << t_shell << " Shell" << std::endl;
std::cout << "Adding " << t_meat << std::endl;
std::cout << "Adding Toppings: " << std::endl;
std::vector<std::string>::iterator toppingsIter;
for(toppingsIter = t_toppings.begin(); toppingsIter != t_toppings.end(); toppingsIter++)
{
std::string topping = *toppingsIter;
std::cout << " " << topping << std::endl; // Print out the topping we just added
}
TacoBell Doriotos Taco Header (tacobelldoritos.h)
#ifndef TACOBELLDORITOS_H_
#define TACOBELLDORITOS_H_
#include "taco.h"
class TacoBellDoritos : public Taco {
public:
TacoBellDoritos();
virtual ~ TacoBellDoritos();
};
#endif
TacoBell Doriotos Taco (tacobelldoritos.cpp)
#include "tacobelldoritos.h"
// Constructor
TacoBellDoritos::TacoBellDoritos() {
t_name = std::string("Taco Bell Doriotos Taco ");
t_shell = std::string("Hard Shell");
t_meat = std:: string("Chicken");
// Toppings
t_toppings.push_back("Cheese");
t_toppings.push_back("Gauc");
t_toppings.push_back("Lettuce");
t_toppings.push_back("Sour Cream");
}
TacoBellDoritos::~TacoBellDoritos() {}
TacoBell Veggie Taco Header (tacobellveggie.cpp)
#ifndef TACOBELLDVEGGIE_H_
#define TACOBELLDVEGGIE_H_
#include "taco.h"
class TacoBellVeggie : public Taco {
public:
TacoBellVeggie();
virtual ~ TacoBellVeggie();
};
#endif
TacoBell Veggie Taco (tacobellveggie.cpp)
#include "tacobellveggie.h"
TacoBellVeggie::TacoBellVeggie() {
t_name = std::string("Taco Bell Veggie Taco ");
t_shell = std::string("Soft Shell");
t_meat = std:: string("Tofu");
// Toppings
t_toppings.push_back("Cheese");
t_toppings.push_back("Salsa");
t_toppings.push_back("Peppers");
t_toppings.push_back("Coby Jack Cheese");
t_toppings.push_back("Gauc");
t_toppings.push_back("Lettuce");
t_toppings.push_back("Sour Cream");
}
TacoBellVeggie::~TacoBellVeggie() {}
TacoBell Regular Taco Header (tacobellregular.h)
#ifndef TACOBELLREGULAR_H_
#define TACOBELLREGULAR_H_
#include "taco.h"
class TacoBellRegular : public Taco {
public:
TacoBellRegular();
virtual ~TacoBellRegular();
};
#endif
TacoBell Regular Taco (tacobellregular.cpp)
#include "tacobellregular.h"
// Constructor
TacoBellRegular::TacoBellRegular() {
t_name = std::string("Taco Bell Regular Taco ");
t_shell = std::string("Soft Shell");
t_meat = std:: string("Steak");
// Toppings
t_toppings.push_back("Goat Cheese");
t_toppings.push_back("Salsa");
t_toppings.push_back("Cheese");
t_toppings.push_back("Lettuce");
t_toppings.push_back("Sour Cream");
}
TacoBellRegular::~TacoBellRegular() {}
TacoBell Header (TacoBell.h)
#ifndef TACOBELL_H_
#define TACOBELL_H_
#include "TacoJoint.h"
class TacoBell : public TacoJoint {
public:
TacoBell();
virtual ~TacoBell();
virtual Taco * CreateTaco(std::string type);
};
#endif
TacoBell (TacoBell.cpp)
#include "TacoBell.h"
#include "tacobelldoritos.h"
#include "tacobellregular.h"
#include "tacobellveggie.h"
TacoBell::TacoBell() {}
TacoBell::~TacoBell() {}
Taco * TacoBell::CreateTaco(std::string type)
{
if (type == "Veggie" )
{
return new TacoBellVeggie();
}
else if (type == "Regular" )
{
return new TacoBellRegular();
}
else
{
return new TacoBellDoritos();
}
}
TacoJoint Header (TacoJoint.h)
#ifndef TACOJOINT_H_
#define TACOJOINT_H_
#include <string>
class Taco;
class TacoJoint {
public:
TacoJoint();
virtual ~TacoJoint();
virtual Taco * CreateTaco(std::string type) = 0;
virtual Taco * OrderTaco(std::string type);
};
#endif
TacoJoint (TacoJoint.cpp)
#include "TacoBell.h"
#include "taco.h"
#include <iostream>
int main()
{
TacoJoint * tacobell = new TacoBell();
Taco * veggietaco = tacobell->OrderTaco(std::string("Veggie"));
std::cout << "I just ordered a " << veggietaco->GetName() << std::endl;
delete veggietaco;
delete tacobell;
TacoJoint * tacobell_ = new TacoBell();
Taco * Doritostaco = tacobell_->OrderTaco(std::string("Doritos"));
std::cout << "I just ordered a " << Doritostaco->GetName() << std::endl;
delete Doritostaco;
delete tacobell_;
// Order a TacoBell Regular
TacoJoint * tacobell_2 = new TacoBell();
Taco * RegularTaco = tacobell_2->OrderTaco(std::string("Regular"));
std::cout << "I just ordered a " << RegularTaco->GetName() << std::endl;
delete RegularTaco;
delete tacobell_2;
}
Output:
Cooking the Taco.....done
Preparing Taco Bell Veggie Taco
Adding the Soft Shell Shell
Adding Tofu
Adding Toppings:
Cheese
Salsa
Peppers
Coby Jack Cheese
Gauc
Lettuce
Sour Cream
Wrapping your Taco Bell Veggie Taco
I just ordered a Taco Bell Veggie Taco
Cooking the Taco.....done
Preparing Taco Bell Doriotos Taco
Adding the Hard Shell Shell
Adding Chicken
Adding Toppings:
Cheese
Gauc
Lettuce
Sour Cream
Wrapping your Taco Bell Doriotos Taco
I just ordered a Taco Bell Doriotos Taco
Cooking the Taco.....done
Preparing Taco Bell Regular Taco
Adding the Soft Shell Shell
Adding Steak
Adding Toppings:
Goat Cheese
Salsa
Cheese
Lettuce
Sour Cream
Wrapping your Taco Bell Regular Taco
I just ordered a Taco Bell Regular Taco