Introduction to design patterns, Factory mode, C + + code implementation

Source: Internet
Author: User

Test04.cpp:Defines the entry point for the console application.
//
Design Pattern 4th Factory model
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace Std;
Class Pizza
{
Public
String name;
string dough;
string sauce;
Vector<string> toppings;

void Prepare ()
{
cout<< "Preparing" <<name<<endl;
cout<< "tossing dough ..." <<endl;
cout<< "Adding sauce ..." <<endl;
cout<< "Adding toppings:" <<endl;
for (int i = 0;i < Toppings.size (); i++)
{
cout<<toppings[i]<<endl;
}
}

void Bake ()
{
cout<< "Bake for minutes at" <<endl;
}

virtual void cut ()
{
cout<< "cutting ths pizza into diagonal slices" <<endl;
}

void Box ()
{
cout<< "Pizza in official Pizzastone box" <<endl;
}

String GetName ()
{
return name;
}
};

Class Pizzastore
{
Public
pizza* Orderpizza (String type)
{
pizza* Pizza;
Pizza = Createpizza (type);

Pizza->prepare ();
Pizza->bake ();
Pizza->cut ();
Pizza->box ();

return pizza;
}

Protected
Virtual pizza* Createpizza (String type) {return null;};/ /The core of the factory model
};

Class Nystylecheesepizza:public Pizza
{
Public
Nystylecheesepizza () {
Name = "NY Style Sauce and Cheese Pizza";
Dough = "Thin crust dough";
sauce = "marinara sauce";

Toppings.push_back ("grated Reggiano Cheese");//covered with Italian Reggiano premium cheese
}
};

Class Chicagostylecheesepizza:public Pizza
{
Public
Chicagostylecheesepizza ()
{
Name = "Chicago Style deep Dish Cheese Pizza";
Dough = "Extra Thick crust Dough";
sauce = "Plum Tomato sauce";

Toppings.push_back ("shredded mozzarella Cheese");
}

void Cut ()
{
cout<< "cutting the pizza into square slices" <<endl;
}
};

Class Nypizzastore:public Pizzastore
{
pizza* Createpizza (String item)
{
if (item = = "Cheese")
{
return new Nystylecheesepizza ();
}
/*else if (item = = "Veggie")
{
return new Nystyleveggiepizza ();
}
else if (item = = "Clam")
{
return new Nystyleclampizza ();
}
else if (item = = "Pepperoni")
{
return new Nystylepepperonipizza ();
}*/
Else
{
return NULL;
}
}
};
Class Chicagopizzastore:public Pizzastore
{
pizza* Createpizza (String item)
{
if (item = = "Cheese")
{
return new Chicagostylecheesepizza ();
}
Else
{
return NULL;
}
}
};

There is also an abstract factory pattern
int _tmain (int argc, _tchar* argv[])
{

pizzastore* Nystore = new Nypizzastore ();
pizzastore* Chicagostore = new Chicagopizzastore ();

pizza* Pizza = Nystore->orderpizza ("cheese");
cout<< "Ethan ordered a" <<pizza->getname () <<endl;

Pizza = Chicagostore->orderpizza ("cheese");
cout<< "Joel ordered a" <<pizza->getname () <<endl;
return 0;
}


Introduction to design patterns, Factory mode, C + + code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.