C++:组织程序子系统的正确方法是什么?

发布于 2024-10-19 23:10:10 字数 444 浏览 7 评论 0原文

假设您有一个大型应用程序项目,代码被分为一系列子系统,这些子系统作为从 CBaseSubsystem 派生的类实现。

我的第一个问题已经出现:

  • 将子系统组织为类(从基类派生)是一个好主意吗?

然后,您就有了子系统类。现在,您需要在某处创建它们的实例。但在哪里呢?

  • 将每个子系统实例存储在全局变量中是个好主意吗,例如:
    extern CEventSystem* g_EventSystem;

  • 实例应该在哪里实际创建?所有这些都在一个类似 main() 的函数中?

  • 或者完全避免全局变量并将实例组织在 CSubsystemManager 类或类似类中更好?

  • 使用单例类是合适的方法吗?

Imagine you have a large application project, the code is split into a range of subsystems implemented as classes derived from CBaseSubsystem.

Here already comes my first question:

  • Is it a good idea to organize subsystems as classes [derived from a base class]?

Then, you have your subsystem classes. Now, you will need create have instances of them somewhere. But where?

  • Is it a good idea to have every subsystem instance stored in a global variable, for example:
    extern CEventSystem* g_EventSystem;

  • Where should the instances be actually created? All together in a main()-like function?

  • Or is it better to avoid global variables entirely and organize the instances in a CSubsystemManager class or the like?

  • Would using singleton classes be an appropriate approach?

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

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

发布评论

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

评论(4

祁梦 2024-10-26 23:10:10

这个非常相似。我会避免使用 Singleton,因为这里绝对没有必要,并且会带来关于许多其他问题

在类似 main 的例程中创建实例。如果某些东西需要系统,请通过某种方式提供对其的访问,无论是直接传递给该东西的参数还是提供对这些系统的访问的容器(IE:CSubSystemManager)。您可能不需要编写 CSubSystemManager。

避免全局状态,这是它被耻辱的原因。

Very similar to this. I'd avoid Singleton, because it is absolutely unnecessary here and brings about many other issues.

Create the instances in a main-like routine. If something needs a system, provide access to it through some means, be it a parameter passed directly to that something or a container that provides access to these systems (IE: CSubSystemManager). You likely don't need to go so far as to write CSubSystemManager.

Avoid global state, there's a reason it has a stigma.

淑女气质 2024-10-26 23:10:10

如果您需要一种方法来访问每个子系统的实例,我会避免使用“extern”,而是使用一个单例来访问 CSubsystemManager 的实例。管理器本身可以负责实例化和管理子系统对象。

If you need a way to access an instance of each subsystem, I would avoid using "extern" and instead go with a singleton that gives you access to an instance of CSubsystemManager. The manager itself can take care of instantiating and managing your subsystem objects.

谜兔 2024-10-26 23:10:10

如果您想谈论理论而不是提供具体问题,请考虑根据 FactorySingleton 以及可能的 子系统中的策略,可以对一系列不同的对象类型实现类似的操作。

主子系统 Factory 本身就是一个单例,我希望其目的是显而易见的。使用延迟计算仅允许根据需要加载所需的子系统对象。您可能需要考虑为每个子系统提供引用计数机制,以便在不再需要时将其删除。一些子系统本身可能是单例和/或也​​可以实现策略模式来为一系列类类型提供特定服务。
这个服务可能是例如验证、渲染、序列化等。

以这种方式构建应用程序是否是一个“好主意”的价值判断不是我能说的。我宁愿让古老的智慧来阐述这一点。

If you want to talk theory rather than provide a concrete question then consider designing in terms of a combination of Factory and Singleton with the possibility of Strategy in subsystems which may implement a similar operation for a range of different object types.

The main subsystem Factory is itself a singleton whose purpose is I hope obvious. Use lazy evaluation to allow only required subsystem objects to be loaded as needed. You may want to consider providing each subsystem with a reference counting mechanism which will allow them to be dropped when no longer needed. Some subsystems may themselves be singletons and/or may also implement Strategy pattern to provide a particular service for a range of class types.
This service may be for example validation, rendering, serialisation etc.

The value judgement of whether it is a "good idea" to build your application in this way is not for me to say. I would rather let older wisdom speak to that.

人海汹涌 2024-10-26 23:10:10

我的解决方案将创建单例注册表、抽象工厂和工厂的组合。

//in base module

class ISubsystem{} //an interface for a subsystem

//in module code
Registry::instance()->registerFactory("subsystem1",new Subsystem1Factory());

//in user code
IFactory* factory = Registry::instance()->getFactory("subsystem1");
ISubsystem* subsystem = factory->createObject(...); //you can implement whatever policy in factory impl. (like singleton etc.)

My solution would create a combo of a singleton Registry, AbstractFactory and Factory.

//in base module

class ISubsystem{} //an interface for a subsystem

//in module code
Registry::instance()->registerFactory("subsystem1",new Subsystem1Factory());

//in user code
IFactory* factory = Registry::instance()->getFactory("subsystem1");
ISubsystem* subsystem = factory->createObject(...); //you can implement whatever policy in factory impl. (like singleton etc.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文