提交 d87a912e 编写于 作者: L liu-jianhao

Factory-Pattern Head First 版

上级 a3b1c252
#ifndef CHICAGOPIZZASTORE_HPP
#define CHICAGOPIZZASTORE_HPP
#include "PizzaStore.hpp"
#include "ChicagoStyleCheesePizza.hpp"
#include "ChicagoStylePepperoniPizza.hpp"
class ChicagoPizzaStore : public PizzaStore {
public:
std::unique_ptr<Pizza> createPizza(std::string type) {
std::unique_ptr<Pizza> pizza = nullptr;
if (type == "cheese")
{
pizza = std::make_unique<ChicagoStyleCheesePizza>();
}
else if (type == "pepperoni")
{
pizza = std::make_unique<ChicagoStylePepperoniPizza>();
}
return pizza;
}
};
#endif
\ No newline at end of file
#ifndef CHICAGOSTYLECHEESEPIZZA_HPP
#define CHICAGOSTYLECHEESEPIZZA_HPP
#include "Pizza.hpp"
class ChicagoStyleCheesePizza: public Pizza
{
public:
ChicagoStyleCheesePizza()
{
name = "Chicago Style Deep Dish Pizza";
dough = "Extra thick crust dough";
sauce = "Plum tomato sauce";
toppings.push_back("Shredded mozzarella");
}
};
#endif
\ No newline at end of file
#ifndef CHICAGOSTYLEPEPPERONIPIZZA_HPP
#define CHICAGOSTYLEPEPPERONIPIZZA_HPP
#include "Pizza.hpp"
class ChicagoStylePepperoniPizza: public Pizza
{
public:
ChicagoStylePepperoniPizza()
{
name = "Chicago Style Pepperoni Pizza";
dough = "Extra thick crust dough";
sauce = "Plum tomato sauce";
toppings.push_back("Shredded mozzarella");
toppings.push_back("Sliced pepperoni");
toppings.push_back("No olives or eggplant because that should not go on a pizza");
}
};
#endif
\ No newline at end of file
all:
g++ -g -Wall main.cpp -o main
clean:
rm main
#ifndef NYPIZZASTORE_HPP
#define NYPIZZASTORE_HPP
#include "PizzaStore.hpp"
#include "NYStyleCheesePizza.hpp"
#include "NYStylePepperoniPizza.hpp"
class NYPizzaStore : public PizzaStore {
public:
std::unique_ptr<Pizza> createPizza(std::string type) {
std::unique_ptr<Pizza> pizza = nullptr;
if (type == "cheese")
{
pizza = std::make_unique<NYStyleCheesePizza>();
}
else if (type == "pepperoni")
{
pizza = std::make_unique<NYStylePepperoniPizza>();
}
return pizza;
}
};
#endif
\ No newline at end of file
#ifndef NYSTYLECHEESEPIZZA_HPP
#define NYSTYLECHEESEPIZZA_HPP
#include "Pizza.hpp"
class NYStyleCheesePizza : public Pizza {
public:
NYStyleCheesePizza()
{
name = "NY Style sauce and cheese pizza";
dough = "Thin crust dough";
sauce = "Marinara sauce";
toppings.push_back("Gratted reggiano cheese");
}
};
#endif
\ No newline at end of file
#ifndef NYSTYLEPEPPERONIPIZZA_HPP
#define NYSTYLEPEPPERONIPIZZA_HPP
#include "Pizza.hpp"
class NYStylePepperoniPizza: public Pizza
{
public:
NYStylePepperoniPizza()
{
name = "NY style pepperoni pizza";
dough = "Thin crust dough";
sauce = "Marinara sauce";
toppings.push_back("Grated reggiano cheese");
toppings.push_back("Sliced pepperoni");
toppings.push_back("Garlic");
toppings.push_back("Onion");
toppings.push_back("Mushrooms");
toppings.push_back("Red pepper");
}
};
#endif
#ifndef PIZZA_HPP
#define PIZZA_HPP
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
class Pizza {
public:
std::string name;
std::string dough;
std::string sauce;
std::vector<std::string> toppings;
const std::string& getName() const;
virtual void prepare() const;
virtual void bake() const;
virtual void cut() const;
virtual void box() const;
virtual ~Pizza() = default;
};
const std::string& Pizza::getName() const
{
return this->name;
}
void Pizza::prepare() const
{
std::cout << "Preparing " + getName() << std::endl;
std::cout << "Tossing dough..." << std::endl;
std::cout << "Adding sauce..." << std::endl;
std::cout << "Adding toppings: " << std::endl;
for (auto& topping : this->toppings)
{
std::cout << " " + topping + "\n";
}
}
void Pizza::bake() const
{
std::cout << "Baking for 25 min at 350 degrees "<< std::endl;
}
void Pizza::cut() const
{
std::cout << "Cut the pizza into diagonal slices "<< std::endl;
}
void Pizza::box() const
{
std::cout << "Boxing in official PizzaStore boxes" << std::endl;
}
std::ostream& operator<<(std::ostream& os, const Pizza& pizza)
{
std::string str;
str = "\n---- " + pizza.getName() + "----\n";
str.append(pizza.dough + "\n");
str.append(pizza.sauce + "\n");
for (auto& topping : pizza.toppings)
{
str.append(" " + topping + "\n");
}
str.append("\n");
return os << str;
}
#endif
\ No newline at end of file
#ifndef PIZZASTORE_HPP
#define PIZZASTORE_HPP
#include "Pizza.hpp"
#include <memory>
class PizzaStore {
public:
std::unique_ptr<Pizza> orderPizza(std::string type) {
auto pizza = createPizza(type);
if(pizza != nullptr)
{
std::cout << "\n--- Making a " + pizza->getName() + " ---\n \n";
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
}
return pizza;
}
virtual std::unique_ptr<Pizza> createPizza(std::string type) = 0;
};
#endif
\ No newline at end of file
## 工厂模式
![](https://img-blog.csdnimg.cn/20190623171420456.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlc3Ricm9va2xpdQ==,size_16,color_FFFFFF,t_70)
\ No newline at end of file
#include "NYPizzaStore.hpp"
#include "ChicagoPizzaStore.hpp"
#include "PizzaStore.hpp"
#include "Pizza.hpp"
int main()
{
auto nyStore = new NYPizzaStore();
auto chicagoStore = new ChicagoPizzaStore();
auto nyCheese = nyStore->orderPizza("cheese");
auto chicagoCheese = chicagoStore->orderPizza("cheese");
return 0;
}
......@@ -52,6 +52,8 @@
+ [Bridge](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Bridge)
### 对象创建:
+ [Factory Method](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Factory%20Method)
+ [Factory(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Factory-Pattern)
+ [Bridge](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Bridge)
+ [Abstract Factory](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Abstract%20Factory)
+ [Prototype](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Prototype)
+ [Builder](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Builder)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册