用于访问管理的中间人对象

Middleman object for access management?

本文关键字:中间人 对象 管理 访问 用于      更新时间:2023-10-16

我有两个类:

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调用getGameObjectsAtaddObject。然而,我不希望GameObject能够调用Worldupdate函数。


我可以创建一个新的类WorldSomething,它包含我的World对象,并具有addObjectgetGameObjectsAt函数。然后,我可以将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()