:工厂模式1:方法模式--Pizza

:工厂模式1:方法模式--Pizza

#ifndef __PIZZA_H__
#define __PIZZA_H__
class Pizza
{
public:
Pizza(){}
virtual ~Pizza(){}
virtual const char* getPizzaName()
{
return "Pizza";
}
}; class NYCheesePizza : public Pizza
{
public:
NYCheesePizza(){}
virtual~NYCheesePizza(){}
virtual const char* getPizzaName()
{
return "NYCheesePizza";
}
}; class NYClamPizza : public Pizza
{
public:
NYClamPizza(){}
virtual~NYClamPizza(){}
virtual const char* getPizzaName()
{
return "NYClamPizza";
}
}; class NYVeggiePizza : public Pizza
{
public:
NYVeggiePizza(){}
virtual~NYVeggiePizza(){}
virtual const char* getPizzaName()
{
return "NYVeggiePizza";
}
}; class ChicagoVeggiePizza : public Pizza
{
public:
ChicagoVeggiePizza(){}
virtual~ChicagoVeggiePizza(){}
virtual const char* getPizzaName()
{
return "ChicagoVeggiePizza";
}
}; class ChicagoClamPizza : public Pizza
{
public:
ChicagoClamPizza(){}
virtual~ChicagoClamPizza(){}
virtual const char* getPizzaName()
{
return "ChicagoClamPizza";
}
}; class ChicagoCheesePizza : public Pizza
{
public:
ChicagoCheesePizza(){}
virtual~ChicagoCheesePizza(){}
virtual const char* getPizzaName()
{
return "ChicagoCheesePizza";
}
}; #endif
#ifndef __PIZZASTORE_H__
#define __PIZZASTORE_H__
#include "Pizza.h"
#include <iostream>
using namespace std;
class PizzaStore
{
public:
PizzaStore()
{
}
virtual~PizzaStore(){}
virtual Pizza* OrderPizza(const char* style)
{
Pizza *p = CreatePizza(style);
cout<<p->getPizzaName()<<endl;
return p;
}
private:
virtual Pizza* CreatePizza(const char* style) = 0
{ }
}; class NYPizzaStore :public PizzaStore
{
public:
NYPizzaStore(){}
virtual ~NYPizzaStore(){}
private:
virtual Pizza* CreatePizza(const char* style)
{
Pizza *p;
if (style == "Cheese")
{
p = new NYCheesePizza();
}
else if (style == "Clam")
{
p = new NYClamPizza();
}
else if (style =="Veggie")
{
p = new NYVeggiePizza();
}
else p = NULL;
return p;
}
}; class ChicagoPizzaStore :public PizzaStore
{
public:
ChicagoPizzaStore(){}
virtual ~ChicagoPizzaStore(){}
private:
virtual Pizza* CreatePizza(const char* style)
{
Pizza *p;
if (style == "Cheese")
{
p = new ChicagoCheesePizza();
}
else if (style == "Clam")
{
p = new ChicagoClamPizza();
}
else if (style == "Veggie")
{
p = new ChicagoVeggiePizza();
}
else p = NULL;
return p;
}
}; #endif
#include "Pizza.h"
#include <iostream>
#include "PizzaStore.h"
using namespace std;
int main()
{
cout << "OK" << endl;
PizzaStore *n = new NYPizzaStore();
Pizza *p = n->OrderPizza("Cheese");
p = n->OrderPizza("Clam"); n = new ChicagoPizzaStore();
p = n->OrderPizza("Cheese");
p = n->OrderPizza("Clam");
return 0;
}
上一篇:Android开发新手学习总结(六)——android开发目录结构【图文版】


下一篇:rnn-手写数字识别-网络结构-shape