使用多个服务器指向的客户端是不是不好的设计?

发布于 2024-12-29 12:44:52 字数 1521 浏览 0 评论 0原文

我正在构建一个游戏引擎,并且正在尝试重新组织一些东西;即EngineManager,它管理引擎的主要组件(音频、视频、键盘/鼠标IO等)。

如果您愿意的话,此清理的主要实现思想是使用 EngineManager 作为客户端,它将通过事件处理(在 SDL 中)向这些不同的对象发送数据。组件,它们将依次发送回消息。为了避免疯狂,我有以下实现:

Config

    class Config
    {
    public:

        Config( const EngineManager* engine, const Controls* controls );

        ~Config( void );

        //More functions

    private:

        Controls** mControls;

        EngineManager** mEngine;

        //More functions/class members

    };

    Config::Config( const EngineManager* engine, const Controls* controls )
        : mControls( controls ),
          mEngine( engine ),
          mIsInitialized( false )
    {
    }

    Config::~Config( void )
    {
        delete mControls;
        delete mEngine;
    }

EngineManager

class EngineManager
{

public:

    EngineManager( void );

    ~EngineManager( void );

    //More functions/class members

private:

    Controls* mControls;

    Config* mConfig;

   //More functions/class members

};

EngineManager::EngineManager( void )
    : mControls( new Controls( this ) ),
      mConfig( new Config( this, mControls ) ),
      mEvent( new SDL_Event )
{
    Init();
}

EngineManager::~EngineManager( void )
{
    delete mEvent;
    delete mConfig;
    delete mControls;
    delete mScreen;
}

我会发布控件,但它基本上与配置类执行相同的操作 - 尽管没有引用配置。

尽管如此,有些事情告诉我,一个好的实现可能是某种函数指针......

有什么想法吗?

I'm building a game engine, and I'm trying to reorganize a few things; namely the EngineManager which manages the main components of the engine (audio, video, keyboard/mouse IO, etc.).

The main implementation idea for this clean up, if you will, is to use the EngineManager as a client which will send data via event-handling (in SDL) to these various components, which will in turn send messages back. To avoid insanity, I have the following implementation:

Config

    class Config
    {
    public:

        Config( const EngineManager* engine, const Controls* controls );

        ~Config( void );

        //More functions

    private:

        Controls** mControls;

        EngineManager** mEngine;

        //More functions/class members

    };

    Config::Config( const EngineManager* engine, const Controls* controls )
        : mControls( controls ),
          mEngine( engine ),
          mIsInitialized( false )
    {
    }

    Config::~Config( void )
    {
        delete mControls;
        delete mEngine;
    }

EngineManager

class EngineManager
{

public:

    EngineManager( void );

    ~EngineManager( void );

    //More functions/class members

private:

    Controls* mControls;

    Config* mConfig;

   //More functions/class members

};

EngineManager::EngineManager( void )
    : mControls( new Controls( this ) ),
      mConfig( new Config( this, mControls ) ),
      mEvent( new SDL_Event )
{
    Init();
}

EngineManager::~EngineManager( void )
{
    delete mEvent;
    delete mConfig;
    delete mControls;
    delete mScreen;
}

I'd post controls, but it basically does the same thing as the config class - though without a reference to config.

Still, something tells me that a good implementation MAY be a function pointer of some sort...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2025-01-05 12:44:52

这里最大的设计问题是相互依赖。如果您使用客户端/服务器,则客户端应该依赖于服务器,而不是相反。

如果客户端需要了解服务器中的更新,您应该使用观察者模式
如果需要对消息进行回复,并且您不想使用同步消息(发送者等待接收者处理消息),那么您应该传递带有消息的回调,这就是函子的作用,因为函子可以包含对您的客户端的某种引用,因此函子可以发回消息。
如果客户端数量有限且不经常更改,您可以将它们传递给服务器,但您应该使用客户端实现的回调接口。

The biggest design issue here is mutual dependency. If you use client/server, the client should depend on the servers and not the other way around.

If clients need to know about updates in servers, you should use the observer pattern.
If a reply on a message is needed and you don't want to use a synchronized message (sender waits until receiver has processed message), then you should pass a callback with your message and here is where the functor comes in as the functor can contain a sort of reference to your client, so the functor can send a message back.
If there are a limited amount of clients which don't change often, you can pass them to the server, but then you should use a callback interface implemented by your client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文