c++設計模式:簡單原廠模式(Simple Factory Pattern)

來源:互聯網
上載者:User

 定義:

從設計模式的類型上來說,簡單原廠模式是屬於建立型模式,又叫做靜態Factory 方法(StaticFactory Method)模式,但不屬於23種GOF設計模式之一。簡單原廠模式是由一個工廠對象決定建立出哪一種產品類的執行個體。簡單原廠模式是原廠模式家族中最簡單實用的模式,可以理解為是不同原廠模式的一個特殊實現。

情境:

我們要開一家比薩商店,定義了一個簡單的披薩工廠,可以根據客戶的需要製作芝士披薩、辣腸比薩、蛤蜊披薩、素食披薩。根據傳入的披薩類型從披薩工廠生產出客戶所需要的披薩。

類圖:

c++代碼如下:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class PizzaType
{
public:
enum EPizzaType{cheese,pepperoni,clam,veggie};
};

class Pizza
{
public:
virtual ~Pizza() {};
string getName();
void prepare();
void bake();
void cut();
void box();
protected:
string name;
string dough;
string sauce;
vector<string> toppings;
};

class CheesePizza : public Pizza
{
public:
CheesePizza();
};

class PepperoniPizza : public Pizza
{
public:
PepperoniPizza();
};

class ClamPizza : public Pizza
{
public:
ClamPizza();
};

class VeggiePizza : public Pizza
{
public:
VeggiePizza();
};

class SimplePizzaFactory
{
public:
Pizza* createPizza(PizzaType::EPizzaType type);
};

class PizzaStore
{
private:
SimplePizzaFactory factory;
public:
Pizza* orderPizza(PizzaType::EPizzaType type);
};


string Pizza::getName()
{
return name;
}
void Pizza::prepare()
{
printf("Preparing %s\n",name.c_str());
}
void Pizza::bake()
{
printf("Baking %s\n",name.c_str());
}
void Pizza::cut()
{
printf("Cutting %s\n",name.c_str());
}
void Pizza::box()
{
printf("Boxing %s\n",name.c_str());
}

CheesePizza::CheesePizza()
{
name = "Cheese Pizza";
dough = "Regular Crust";
sauce = "Marinara Pizza Sauce";
toppings.push_back("Fresh Mozzarella");
toppings.push_back("Parmesan");
}

PepperoniPizza::PepperoniPizza()
{
name = "Pepperoni Pizza";
dough = "Crust";
sauce = "Marinara sauce";
toppings.push_back("Sliced Pepperoni");
toppings.push_back("Sliced Onion");
toppings.push_back("Grated parmesan cheese");
}

ClamPizza::ClamPizza()
{
name = "Clam Pizza";
dough = "Thin crust";
sauce = "White garlic sauce";
toppings.push_back("Clams");
toppings.push_back("Grated parmesan cheese");
}

VeggiePizza::VeggiePizza()
{
name = "Veggie Pizza";
dough = "Crust";
sauce = "Marinara sauce";
toppings.push_back("Shredded mozzarella");
toppings.push_back("Grated parmesan");
toppings.push_back("Diced onion");
toppings.push_back("Sliced mushrooms");
toppings.push_back("Sliced red pepper");
toppings.push_back("Sliced black olives");
}

Pizza* SimplePizzaFactory::createPizza(PizzaType::EPizzaType type)
{
Pizza* pizza = NULL;
if (PizzaType::cheese == type)
{
pizza = new CheesePizza();
}
else if (PizzaType::pepperoni == type)
{
pizza = new PepperoniPizza();
}
else if (PizzaType::clam == type)
{
pizza = new ClamPizza();
}
else if (PizzaType::veggie == type)
{
pizza = new VeggiePizza();
}
return pizza;
}

Pizza* PizzaStore::orderPizza(PizzaType::EPizzaType type)
{
Pizza* pizza = NULL;
pizza = factory.createPizza(type);
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
return pizza;
}

int main()
{
PizzaStore pizzaStore;
Pizza* pCheesePizza = pizzaStore.orderPizza(PizzaType::cheese);
Pizza* pPepperoniPizza = pizzaStore.orderPizza(PizzaType::pepperoni);
Pizza* pClamPizza = pizzaStore.orderPizza(PizzaType::clam);
Pizza* pVeggiePizza = pizzaStore.orderPizza(PizzaType::veggie);

delete pCheesePizza;
delete pPepperoniPizza;
delete pClamPizza;
delete pVeggiePizza;

return 0;
}

 

運行後結果如下:

Preparing Cheese Pizza
Baking Cheese Pizza
Cutting Cheese Pizza
Boxing Cheese Pizza
Preparing Pepperoni Pizza
Baking Pepperoni Pizza
Cutting Pepperoni Pizza
Boxing Pepperoni Pizza
Preparing Clam Pizza
Baking Clam Pizza
Cutting Clam Pizza
Boxing Clam Pizza
Preparing Veggie Pizza
Baking Veggie Pizza
Cutting Veggie Pizza
Boxing Veggie Pizza

 

參考圖書:《Head First 設計模式》

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.