- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的游戏编程入门类(class)即将结束,我想结合我在类里面学到的技能和我之前的 OOP 经验来创建一个用于制作 2D 游戏的小型库。不过,我目前担心的是,这是管理我的类实例集合的最佳方式。
我正在使用的库 (DarkGDK) 完全由作用于整数的自由函数组成。当使用诸如 dbSprite() 之类的函数创建“对象”时,您会为它指定一个唯一 ID(int
值)以引用它——我猜是某种“地址”。我个人觉得这种方法很糟糕,所以我创建了类来封装每组自由函数,例如 Image、Sprite 和 AnimatedSprite(这两种 Sprite 类型在 DarkGDK 库中是不同的。)
问题是,为了让这些对象工作,我仍然必须将唯一 ID 传递给构造函数,以便针对适当的地址调用 DarkGDK 函数。我试图摆脱通过 ID 一起引用这些东西,但我正在讨论应该如何创建对象。目前我有一些 AssetManager 类保存对创建的每个对象的引用,检查现有 ID 并只允许唯一的 ID,但这仍然没有解决被迫在管理类外部生成 ID 的问题。这让我认为工厂是最好的方法。
我知道在 C# 中我可以创建一个 AssetFactory<T> where T:Asset
可以轻松地为每个 Assets 调用适当的构造函数来创建实例,但据我所知,C++ 没有这样的设施。
所以我认为我应该采取的方法是使用某种抽象的 AssetFactory。 我 的想法(正确与否)是 AssetFactory 的子级将跟踪正在使用的 ID,并且只发布适当的对象唯一 ID。像这样:
class Asset {
int m_index;
Asset(int index);
};
class Image : public Asset {
Image(char* imgPath);
void Draw();
};
class Sprite : public Asset {
Sprite(Image* img);
void Draw();
};
class AssetFactory {
private:
std::vector<Asset*> m_assets;
int GetUniqueID();
public:
AssetFactory();
~AssetFactory();
virtual Asset* CreateAsset(); // but each class has different constructor parameters...
};
class ImageFactory : public AssetFactory {
Asset* CreateAsset(char* imgPath); // ...so this wouldn't work (nor compile)
};
class SpriteFactory : public AssetFactory {
Asset* CreateAsset(); // ...so will i be forced to call the default constructor and modify it later?
};
这里的问题是,如上所述,不同的对象有不同的构造函数,使这个设计没有实际意义。我应该采取不同的方法吗?还是我对工厂模式的理解有误?
编辑:为了澄清,我想要为 Sprites 和 Images 单独工厂的原因是 允许 Sprite 和图像具有相同的 ID。 ID 只能在相同“类型”的其他 Assets 中是唯一的。
最佳答案
如果您的库允许任意 ID 并且您在相对相等的地址空间(例如 sizeof(int) == sizeof(int*))中工作,那么这是一个需要解决的非常微不足道的问题,这几乎适用于所有我知道的 32 位编译器。然后生成 ID 就很简单了——只需重新解释_cast 指针即可。
class Sprite {
int GetUniqueID() { return reinterpret_cast<int>(this); } // easies
public:
// public interface
};
此外,重新使用旧 ID 实际上可能不值得。只是继续制作新的。我的意思是,您不会用完 32 位整数的空间。
最后,你绝对不能在这里使用运行时继承。如果必须,请使用编译时混入。
关于c++ - 对象管理——容器还是工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7738819/
我会尽可能地解释我正在做的事情,以获得最好的可能的建议/解决方案。这一切都是在 java 中完成的。 我的客户有一个基于 SWING 的桌面应用程序,它将使用 WebStart 加载。我被指派为用户帐
看来这个page包含 Azure CLI 支持的与 Azure API 管理相关的所有功能。但它没有展示如何使用 Azure CLI 管理用户、产品、证书、订阅和 API 等实体。 Azure CLI
我设置了一个 Hadoop 1.2.x 版本,双节点集群。第一节点(NameNode、Jobtracker)和第二节点(Secondary NameNode、Datanode、TaskTracker)
对于内容驱动的网站,设计好坏的关键是关系型数据库。在这个教程中,我们已经使用了MySQL关系型数据库管理系统(RDBMS)建立了我们的数据库。对于网站的开发者来说,MySQL是一个较受欢迎的选择,这
在尝试运行MariaDB之前,首先确定其当前状态,运行或关闭。 有三个选项用于启动和停止MariaDB – 运行mysqld(MariaDB脚本)。 运行mysqld_safe启动脚本。
我在管理界面中遇到 StackedInlines 前缀的问题。我会尝试发布所有必要的代码。 models.py(简要) ##### Base classes class BaseItem(models
我是新来的。到目前为止,我一直在使用 MVC 模型并使用基本的 session 管理模型,即在 session 中存储一个 token 并检查每个请求。 我正在尝试对lift做同样的事情,但我的 se
我在 win 服务中使用 NHiberante。有时我得到 System.ObjectDisposedException: Session is closed! Object name: 'ISess
我正在尝试使用 HtmlUnit 登录 Facebook 页面并查看其 HTML 内容。我正在尝试通过 HtmlUnit 填写登录凭据,但在单击提交按钮时我没有看到正在执行的 session 。 在
我正在为一个相当大的项目开发一个带有 reactjs 的前端,该项目有两个主要接口(interface)。主站点的前端和管理员的前端。 我应该将它们开发为两个不同的项目还是 reactjs 中的一个项
短版 我有一个使用插件基础结构的应用程序。插件具有可配置的属性,可帮助它们了解如何完成工作。插件按配置文件分组以定义如何完成任务,配置文件存储在由 DataContractSerializer 序列化
如何管理 iPhone 应用程序中的用户 session ?我在应用程序的第一页上从用户那里获取了用户名和密码。用户可以随时注销。如何像其他 Web 应用程序一样在 iPhone 应用程序中存储 se
我正在使用 Azure API 管理,其中包含第三方论坛 (Discourse) 的链接。 api管理提供的默认登录系统用于注册用户。我想知道是否可以对 api 管理和论坛使用单点登录,这样用户就不必
我正在使用 Wordpress 建立一个网站,并且我想利用它的 session 。但我没有找到任何插件,甚至文档。在我开始破解之前有什么建议或引用吗? 注意:我问的是 WP 是否以及如何使用标准 PH
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我有一个内联表单集,我想排除一些模型对象在表单集中显示。 例如。模型 B 具有模型 A 的外键,因此它是 1:n(A 对象有许多 B 对象)关系。现在在 A 管理编辑页面上,我已经获得了 B 的内联。
我正在开发一个基于 session 的项目。我在想,与银行类似,我会创建一张支票并为用户提供阻止 session 超时的能力。 我正在考虑创建一个 setInterval 来检查需要身份验证的空白页面
我正在为一位拥有 Magento 商店的客户工作。里面塞满了产品,但这些产品的名称有点乱。他并没有坚持一种命名约定,而是多年来使用了不同的约定。因此,每当他使用“管理”->“管理产品”部分中的“名称”
我使用大约十几个 XSLT 文件来提供大量输出格式。目前,用户必须知道导出的文件格式的扩展名,例如RTF、HTML、TXT。 我还想使用参数来允许更多选项。如果我可以将元数据嵌入 XSL 文件本身,那
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我是一名优秀的程序员,十分优秀!