|
-
June 29th, 2010, 09:46 PM
#1
Prototype Patterns Question
1. What is dynamic loading ?
2. Difference between Dynamic Loading and reduce number of classes ?
AFAIK, reduce the number of sub classing means you don't actually code the class statically but you create the object with different properties in order to avoid create many sub subclass. My registerVehicle and unregisterVehicle() are serve this purpose.
Code:
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";
}
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";
}
Thanks for your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|