What is your opinion about my code ?
#ifndef PROTOTYPE_H
#define PROTOTYPE_H
#include <map>
// =============================================
class prototype
{
public:
prototype();
~prototype();
virtual prototype* clone(prototype* ) = 0;
virtual void initialize(const bool&, const int&,
const std::string& ) = 0;
virtual bool getHasTurbo() const = 0 ;
virtual int getCapacity() const = 0;
virtual const std::string& getCategory() const = 0;
virtual void display() const = 0;
};
// =============================================
#endif
#include "Prototype.h"
// =============================================
prototype::prototype()
{
}
// =============================================
prototype::~prototype()
{
}
#ifndef VEHICLE_PROTOTYPE_H
#define VEHICLE_PROTOTYPE_H
#include "Prototype.h"
#include <string>
// ==============================================
class vehiclePrototype : public prototype
{
public:
vehiclePrototype();
vehiclePrototype(const bool&, const int&,
const std::string&);
vehiclePrototype(prototype* );
~vehiclePrototype();
prototype* clone(prototype* );
void initialize(const bool&, const int&,
const std::string& );
bool getHasTurbo() const;
int getCapacity() const;
const std::string& getCategory() const;
void display() const ;
private:
int capacity;
bool hasTurbo;
std::string category;
};
// =============================================
#endif
#include "VehiclePrototype.h"
#include <iostream>
// =============================================
vehiclePrototype::vehiclePrototype()
: capacity(0), hasTurbo(bool() ),
category(std::string() )
{
}
// =============================================
vehiclePrototype::vehiclePrototype(const bool& userHasTurbo,
const int& userCapacity,
const std::string& userCategory)
{
hasTurbo = userHasTurbo;
capacity = userCapacity;
category = userCategory;
}
// ============================================
vehiclePrototype::vehiclePrototype(prototype* rhs)
{
hasTurbo = rhs->getHasTurbo();
capacity = rhs->getCapacity();
category = rhs->getCategory();
}
// ============================================
vehiclePrototype::~vehiclePrototype()
{
}
// =============================================
prototype* vehiclePrototype::clone(prototype* myPrototype)
{
return new vehiclePrototype(myPrototype);
}
// =============================================
void vehiclePrototype::initialize(const bool& userHasTurbo,
const int& userCapacity,
const std::string& userCategory)
{
hasTurbo = userHasTurbo;
capacity = userCapacity;
category.assign(userCategory);
}
// =============================================
bool vehiclePrototype::getHasTurbo() const
{
return hasTurbo;
}
// =============================================
int vehiclePrototype::getCapacity() const
{
return capacity;
}
// =============================================
const std::string& vehiclePrototype::getCategory() const
{
return category;
}
// =============================================
void vehiclePrototype::display() const
{
std::cout << std::boolalpha
<< "Car Specification\n"
<< "Vehicle Category Type : " << getCategory() << "\n"
<< "Vehicle Capacity : " << getCapacity() << "\n"
<< "Vehicle Turbo : " << getHasTurbo() << "\n";
}
// =============================================
#ifndef PROTOTYPE_MANAGER_H
#define PROTOTYPE_MANAGER_H
#include <map>
#include <vector>
class prototype;
// =============================================
class prototypeManager
{
public:
typedef std::map<int, prototype* > prototypeMap;
typedef std::map<int, prototype* >::iterator prototypeMapIte;
typedef std::vector<prototype*> prototypeVec;
public:
prototypeManager();
~prototypeManager();
prototype* createVehicle(int, const bool&,
const int&, const std::string& );
void populateVehicle();
// To create a specific instance of a class
// without coding the class
void registerVehicle( const bool&,
const int&, const std::string&);
void registerVehicle(const int&, const bool&,
const int&, const std::string&);
void unRegisterVehicle(int);
private:
static int vehicleType;
prototypeMap registry;
// Static Product
// Empty Prototype
prototype* obj;
prototype* sedan, *superCar, *f1Car;
// Dynamic Product
prototypeVec cont;
};
// =============================================
#endif
#include "PrototypeManager.h"
#include "VehiclePrototype.h"
#include <iostream>
// =============================================
int prototypeManager::vehicleType = 1;
// =============================================
prototypeManager::prototypeManager()
: registry(prototypeMap()), obj(new vehiclePrototype),
sedan(new vehiclePrototype(false, 1600, "B Class") ),
superCar(new vehiclePrototype(true, 3000, "D Class") ),
f1Car(new vehiclePrototype(true, 6000, "F Class") ),
cont(prototypeVec() )
{
populateVehicle();
}
// =============================================
prototypeManager::~prototypeManager()
{
delete obj;
delete sedan;
delete superCar;
delete f1Car;
obj = 0;
sedan = 0;
superCar = 0;
f1Car = 0;
for (size_t loop = 0;loop<cont.size();++loop)
{
delete cont[loop];
cont[loop] = 0;
}
}
// =============================================
prototype* prototypeManager::createVehicle(
int uservehicleType,
const bool& userHasTurbo,
const int& userCapacity,
const std::string& userCategory)
{
prototypeMapIte myIte = registry.find(uservehicleType);
prototype* instance = 0;
if (myIte == registry.end() )
{
// Register Vehicle
registerVehicle(uservehicleType, userHasTurbo,
userCapacity, userCategory);
myIte = registry.find(uservehicleType);
prototype* temp = myIte->second;
instance = obj->clone(temp);
instance->initialize(userHasTurbo, userCapacity,
userCategory);
}
else
{
prototype* temp = myIte->second;
instance = obj->clone(temp);
instance->initialize(userHasTurbo, userCapacity,
userCategory);
}
std::cout << "\nClone Vehicle\n";
return instance;
}
// =============================================
void prototypeManager::populateVehicle()
{
registry.insert(prototypeMap::value_type(vehicleType, sedan) );
++vehicleType;
registry.insert(prototypeMap::value_type(vehicleType, superCar) );
++vehicleType;
registry.insert(prototypeMap::value_type(vehicleType, f1Car) );
++vehicleType;
}
// =============================================
void prototypeManager::registerVehicle(
const bool& userHasTurbo,
const int& userCapacity,
const std::string& userCategory)
{
prototype* temp = new vehiclePrototype(userHasTurbo,
userCapacity, userCategory);
cont.push_back(temp);
registry.insert(prototypeMap::value_type(vehicleType, temp) );
++vehicleType;
std::cout << "\nRegister new Vehicle Type "
<<vehicleType << "\n";
}
// =============================================
void prototypeManager::registerVehicle(const int& userVehicleTpye,
const bool& userHasTurbo,
const int& userCapacity,
const std::string& userCategory)
{
prototype* temp = new vehiclePrototype(userHasTurbo,
userCapacity, userCategory);
cont.push_back(temp);
registry.insert(prototypeMap::value_type(userVehicleTpye, temp) );
std::cout << "\nRegister new Vehicle Type "
<<userVehicleTpye << "\n";
}
// =============================================
void prototypeManager::unRegisterVehicle(int vehicleType)
{
prototype* removePrototype = registry.find(vehicleType)->second;
registry.erase(vehicleType);
std::cout << "\nUnRegister Vehicle Type "
<< vehicleType << "\n";
}
// =============================================
#include <iostream>
using namespace std;
#include "Prototype.h"
#include "VehiclePrototype.h"
#include "PrototypeManager.h"
// =============================================
// =============================================
// =============================================
int main()
{
prototypeManager obj;
prototype* myCar;
myCar = obj.createVehicle(1, false, 1300, "B Class");
myCar->display();
myCar = obj.createVehicle(2, true, 3200, "D Class");
myCar->display();
myCar = obj.createVehicle(5, false, 2500, "E Class");
myCar->display();
obj.unRegisterVehicle(1);
myCar = obj.createVehicle(1, false, 1600, "B Class");
myCar->display();
return 0;
}
How to dynamic loading in C++ ? I read the Gof book and i don;t understand the third consequences (pg 120) specifying new object by varying structure. Please explain.
Thanks.