用于访问管理的中间人对象
Middleman object for access management?
我有两个类:
class World {
public:
void update();
void addObject(GameObject* obj);
std::vector<GameObject const*> getGameObjectsAt(float x,float y,float radius) const;
private:
std::vector<GameObject*> gameObjects
}
class GameObject {
public:
void update();
}
现在我希望GameObject
能够在update
函数中从World
调用getGameObjectsAt
和addObject
。然而,我不希望GameObject
能够调用World
的update
函数。
我可以创建一个新的类WorldSomething
,它包含我的World
对象,并具有addObject
和getGameObjectsAt
函数。然后,我可以将WorldSomething
对象传递给GameObject
更新函数:
class World {
public:
void update();
private:
std::vector<GameObject*> gameObjects
}
class WorldSomething {
public:
void addObject(GameObject* obj);
std::vector<GameObject const*> getGameObjectsAt(float x,float y,float radius) const;
private:
World* world;
}
class GameObject {
public:
void update(const WorldSomething& worldSomething);
}
然而,这使我的操作远离我的数据,我必须将我的WorldSomething对象传递给GameObject的更新函数。有没有更好的解决方案?(如果有帮助的话,我正在使用c++…)另外,你怎么称呼这样的WorldSomething
类?
然而,这使我的操作远离我的数据…
这是我们想要的
首先使用接口来解耦逻辑和数据:
struct IWorldAdd {
virtual void addObject(GameObject* obj) = 0;
virtual std::vector<GameObject const*> getGameObjectsAt(float x,float y,float radius)
const = 0;
virtual ~IWorldAdd() {};
};
class World : public IWorldAdd{
public:
void update();
void addObject(GameObject* obj);
std::vector<IGameObject const*> getGameObjectsAt(float x,float y,float radius) const;
private:
std::vector<GameObject*> gameObjects
};
struct IGameObject {
virtual void update() = 0;
virtual Point getCenterPosition() = 0;
virtual ~IGameObject() {}
};
添加一个策略,提供以下逻辑
template<class UpdateStrategy>
class GameObject : public IGameObject {
public:
GameObject(UpdateStrategy* strategy) : strategy_(strategy)
void update() {
// Do stuff needed to update the things directly related to the
// GameObject's rendering and such
// Call the strategy needed to update a particular game object
// in Relation to other objects nearby
strategy_->updateRestOfWorld();
}
private:
UpdateStrategy* strategy_;
};
class AConcreteUpdateStrategy {
AConcreteUpdateStrategy(IWorldAdd* world) : world_(world) {
}
void updateRestOfWorld(const IGameObject* gameObject) {
Point centerPoint gameObject->getCenterPosition();
std::vector<const IGameObject*> otherGameObjects =
world_->getGameObjectsAt(centerPoint.x, centerPoint.x, 500 );
// Inspect if there are specific game objects nearby
if(gameObjectIsNearBy) {
world->addObject(new OtherGameObject());
}
}
private:
IWorldAdd* world_;
};
这是一个粗略的草图,希望你能理解。
看来你的World类负责两个不同的方面。你看,当你认为你的代码的某些部分应该只使用你的World类的"一半";那么也许你的World太大了?
换句话说:创造一个包含游戏对象所需功能的类HalfOfTheWorld是否合理?然后你可能有AnotherHalfOftheWorld传递其他方面;如果需要的话;然后,您的World类可以包含每个新类的一个成员。
有更好的解决方案吗?
我认为最好还是让这些课保持原样。
我不认为添加另一个层(或更多层)只是为了防止GameObject
能够调用World::update()
有任何好处。
程序员纪律是解决方案。确保您的开发人员很好地理解您的执行流程。如果执行流程不需要,则不会从GameObject
调用World::update()
。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 用于访问管理的中间人对象