C++ C++;原型与动载荷
你对我的代码有什么看法C++ C++;原型与动载荷,c++,C++,你对我的代码有什么看法 #ifndef PROTOTYPE_H #define PROTOTYPE_H #include <map> // ============================================= class prototype { public: prototype(); ~prototype(); virtual prototype* clone(prototype* ) = 0; virtual void initialize(c
#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;
}
\ifndef原型
#定义原型
#包括
// =============================================
类原型
{
公众:
原型();
~prototype();
虚拟原型*克隆(原型*)=0;
虚拟无效初始化(常量bool&,常量int&,
常量std::string&=0;
虚拟布尔getHasTurbo()常量=0;
虚拟int getCapacity()常量=0;
虚拟常量std::string&getCategory()常量=0;
虚空显示()常量=0;
};
// =============================================
#恩迪夫
#包括“Prototype.h”
// =============================================
prototype::prototype()
{
}
// =============================================
原型::~prototype()
{
}
#ifndef车辆原型
#定义车辆原型
#包括“Prototype.h”
#包括
// ==============================================
类别车辆原型:公共原型
{
公众:
车辆原型();
车辆原型(常量布尔和,常量整数和,
常量std::字符串&);
车辆原型(原型*);
~vehiclePrototype();
原型*克隆(原型*);
无效初始化(常量bool&,常量int&,
常量std::字符串&);
bool gethastrbo()常量;
int getCapacity()常量;
常量std::string&getCategory()常量;
void display()常量;
私人:
国际能力;
布尔哈斯图博;
std::字符串类别;
};
// =============================================
#恩迪夫
#包括“VehiclePrototype.h”
#包括
// =============================================
vehiclePrototype::vehiclePrototype()
:容量(0),hasTurbo(bool()),
类别(std::string())
{
}
// =============================================
vehiclePrototype::vehiclePrototype(const bool&userHasTurbo,
const int和userCapacity,
const std::string和userCategory)
{
hastrbo=userhastrbo;
容量=用户容量;
类别=用户类别;
}
// ============================================
车辆原型::车辆原型(原型*rhs)
{
hasTurbo=rhs->getHasTurbo();
容量=rhs->getCapacity();
category=rhs->getCategory();
}
// ============================================
vehiclePrototype::~vehiclePrototype()
{
}
// =============================================
prototype*车辆原型::克隆(prototype*myPrototype)
{
返回新车辆原型(myPrototype);
}
// =============================================
void vehiclePrototype::initialize(const bool和userhasburbo,
const int和userCapacity,
const std::string和userCategory)
{
hastrbo=userhastrbo;
容量=用户容量;
category.assign(userCategory);
}
// =============================================
bool vehiclePrototype::getHasTurbo()常量
{
返回涡轮增压器;
}
// =============================================
int vehiclePrototype::getCapacity()常量
{
返回能力;
}
// =============================================
常量std::string&vehiclePrototype::getCategory()常量
{
退货类别;
}
// =============================================
void vehiclePrototype::display()常量
{
std::cout我没有看到任何动态加载的东西
你对我的代码有什么看法
这不是一个代码审查网站,问题和答案都是客观的,所以我只会提到代码中的几个问题。一般来说,你应该就你遇到的具体问题提问,并且只发布描述或重现问题所需的代码
我能看到的唯一错误(在简要阅读之后)是prototype
需要一个虚拟析构函数,否则通过基类指针删除派生类的实例是无效的(就像prototypeManager
的析构函数那样)
风格上,有一些不必要的代码。prototype
不需要构造函数;隐式的就可以了。它确实需要一个析构函数(如上所述,是虚拟的);因为它是空的,所以没有理由不把它内联到类声明中。在初始化列表中,category(std::string())
可以是category()
,也可以完全省略;hasTurbo(bool())
同样可以是hasTurbo()
,或者hasTurbo(false)
,如果您想显式的话。编辑:另外,在析构函数中删除指针后,不需要将其置空。这些都不是特别糟糕的(如果你不得不遵循一种死板的编码风格,这有时是必要的),但它们确实使代码更难理解
如何在C++中动态加载?
我不知道你所说的“动态加载”是什么意思,也不知道它与代码或原型模式的关系。也许如果你解释一下你想要实现什么,有人可以帮助你
我读过Gof的书,但我不理解第三个结果(第120页),即通过改变结构来指定新对象。请解释
假设你有和我相同的版本,那篇文章说你可以把简单的对象组合成复杂的复合对象,只要复合对象正确地实现了原型接口,新的对象就可以和旧的对象一起使用。关于如何做到这一点,没有太多的细节,但是复合模式可能会对于某些应用程序很有用。在您的示例中,您可以想象能够使用一些简单的组件(车轮、发动机、车门、毛绒骰子等)并将它们组合到一辆新车中;只要该车能够正确关闭,那么它就可以像现有的原型一样使用。我将添加一个cont.clear()
在~prototypeManager
中的for循环之后
我不太喜欢当前的createVehicle
函数。它似乎会带来灵活性问题。如果将来实现发生变化,您需要更改多少代码?也许构造函数加setter函数会是一个更好的选择。关于这一点,请看您正在研究设计模式s
prototype::prototype()
{
}
typedef std::map<int, prototype* > prototypeMap;
typedef std::map<int, prototype* >::iterator prototypeMapIte;
for (size_t loop = 0;loop<cont.size();++loop)
{
delete cont[loop];
cont[loop] = 0;
}
prototype* temp = myIte->second;
instance = obj->clone(temp);
registry.insert(prototypeMap::value_type(vehicleType, sedan) );
++vehicleType;
std::cout << "\nRegister new Vehicle Type " <<vehicleType << "\n";
registry.erase(vehicleType);