gpt4 book ai didi

C++ 原型(prototype)和动态加载

转载 作者:行者123 更新时间:2023-11-30 03:10:26 25 4
gpt4 key购买 nike

您对我的代码有何看法?

#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;
}

如何在 C++ 中动态加载?我读了 Gof 的书,但我不明白第三个后果(第 120 页)通过改变结构指定新对象。请解释。

谢谢。

最佳答案

What is your opinion about my code ?

这不是一个代码审查网站,问题和答案都是客观的,所以我只提几个代码问题。通常,您应该针对遇到的具体问题提出问题,并且只发布描述或重现问题所需的代码。

我能看到的唯一错误(在简单阅读之后)是 prototype需要一个虚拟析构函数,否则通过基类指针删除派生类的实例是无效的(如 prototypeManager 的析构函数)。

在风格上,有一些不必要的代码。 prototype不需要构造函数;隐式的就好了。它确实需要一个析构函数(虚拟的,如上所述);因为这将是空的,所以没有理由不将它内联到类声明中。在您的初始化列表中,category(std::string())可能是 category() , 或完全省略; hasTurbo(bool())同样可以是hasTurbo() , 或 hasTurbo(false)如果你想明确。编辑:此外,在析构函数中删除指针后无需使它们无效。这些都不是特别糟糕(如果您必须遵循脑死亡的编码风格,有时是必要的),但它们确实使代码更难遵循。

How to dynamic loading in C++ ?

我不知道“动态加载”是什么意思,也不知道它与代码或原型(prototype)模式有何关系。也许如果您解释了您想要实现的目标,有人可以提供帮助。

I read the Gof book and i don;t understand the third consequences (pg 120) specifying new object by varying structure. Please explain.

假设你的版本和我一样,那段话说你可以将简单的对象组合成复杂的复合对象,只要复合对象正确实现了 Prototype 接口(interface),新对象就可以和旧对象一起使用。它没有详细介绍如何做到这一点,但 Composite 模式可能对某些应用程序有用。在您的示例中,您可以想象能够采用一些简单的组件(轮子、发动机、车门、毛茸茸的骰子等)并将它们组合成一辆新车;只要该车辆可以正确克隆,就可以像现有原型(prototype)一样使用。

关于C++ 原型(prototype)和动态加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3131302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com