0% found this document useful (0 votes)
62 views

Factory Method Design Pattern: Create Motorbike

Here is the code with multiple factories (Phone Factory, Book Factory) that link to an abstract factory: //Abstract Factory class AbstractFactory { public: virtual Product* CreateProduct() = 0; }; //Concrete Factories class PhoneFactory : public AbstractFactory { public: Phone* CreateProduct() { return new Phone; } }; class BookFactory : public AbstractFactory { public: Book* CreateProduct() { return new Book; } }; class VehicleFactory : public AbstractFactory { public: Vehicle* CreateProduct() { return new Vehicle; } }; //Products class Phone { //...
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Factory Method Design Pattern: Create Motorbike

Here is the code with multiple factories (Phone Factory, Book Factory) that link to an abstract factory: //Abstract Factory class AbstractFactory { public: virtual Product* CreateProduct() = 0; }; //Concrete Factories class PhoneFactory : public AbstractFactory { public: Phone* CreateProduct() { return new Phone; } }; class BookFactory : public AbstractFactory { public: Book* CreateProduct() { return new Book; } }; class VehicleFactory : public AbstractFactory { public: Vehicle* CreateProduct() { return new Vehicle; } }; //Products class Phone { //...
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Factory Method Design Pattern

Create Motorbike

Create Me a Plane

Create Car

Create Bike
Create Plane

1
Factory Class Structure

2
Factory Source //Factory class
class Factory{
public:
Vechicle *GetVechicle(VechicleType VT){
Vechicle *vechicle = NULL;
#include <iostream> switch (VT){
using namespace std; case PLANE:
vechicle = new Plane();break;
enum VechicleType{PLANE, BIKE, MOTORBIKE, CAR}; case BIKE:
vechicle = new Bike();break;
//Base class
case MOTORBIKE:
class Vechicle{ vechicle = new Motorbike(); break;
case CAR:
public:
vechicle = new Car; break;
virtual void PrintVechicle()=0; default: break;
}
};
return vechicle;
class Car: public Vechicle{ };
};
public:
int main(){
void PrintVechicle(){ cout<<"Here is Car";} //Create Factory
Factory *VechicleFactory = new Factory();
};
//Factory creates an object of type CAR
class Bike: public Vechicle{ Vechicle *Vech = VechicleFactory->GetVechicle( CAR );
Vech->PrintVechicle();
public:
//Factory creates an object of type CAR
void PrintVechicle(){ cout<<"Here is Bike"; } Vech = VechicleFactory ->GetVechicle (PLANE);
Vech->PrintVechicle();
};
int i;
class Plane: public Vechicle{ std::cin>> i;
return 0;
public: }
void PrintVechicle(){ cout<<"Here is Plane";}
}; 3
In the above sample code, there is only one Factory of vehicle.
Please create a few factories (Phone Factory, Book Factory), and link it
with Abstract Factory (Vehicle Factory also links to Abstract Factory).

Deadline: 14 December 2016

You might also like