Factory Method Design Pattern: Create Motorbike
Factory Method Design Pattern: Create Motorbike
Factory Method Design Pattern: Create Motorbike
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).