C ++:是否应该使用全局变量或类成员指针在模块之间进行通信? [英] C++: Should I use global variables or class member pointers to communicate between modules?

查看:126
本文介绍了C ++:是否应该使用全局变量或类成员指针在模块之间进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有多个作为类组织的子系统。

In my project, I have multiple subsystems organized as classes.

我需要这些类进行通信(以便能够通过指针访问另一个子系统),我想尽可能以最佳方式实施。

I need those classes to communicate (so be able to access the other one via a pointer), and I want to implement this in the best way as possible.

我基本上在这里看到了三种可能的解决方案:

I basically see three possible solutions here:


  • 如果子系统X需要访问子系统Y,则在类X中添加一个指向Y实例的成员变量。创建X时,将指向Y的指针传递给它,并获得该成员变量 m_pSystemY 集。

声明全局变量 CSystemX * g_SystemX 每个子系统。它将在程序启动时充满指向新创建的子系统实例的指针。稍后,您可以轻松地从任何地方访问它。

Declare a global variable CSystemX * g_SystemX for every subsystem. It will be filled with a pointer to the freshly created subsystem instance on program start. Later on, you can easily access it from anywhere.

创建一个复杂的子系统管理器类。所有子系统都存储在一个数组中。您需要调用一个函数才能访问特定的子系统。

Create a sophisticated subsystem manager class. All subsystems are stored in an array. You need to call a function in order to access a specific subsystem.

我的问题:


  • 我的游戏引擎项目应使用以下哪个解决方案?

  • Which one of these solutions should I use for my game engine project?

你们中的任何人是否有使用这些方法的亲身经历?

Does anybody of you have personal experience with any of these methods?

推荐答案

多么有趣,我在每个方向上看到一个答案!

How funny, I see one answer in each direction!

无论如何,我向您推荐可以在选项#1(向类X添加一个成员变量,指向Y的实例)之后进行以下较小的更正:改用接口

Anyway, what I recommend you to do it following option #1 (add a member variable to class X pointing to an instance of Y) with a small correction: use an interface instead.

因此,每个模块都有一个接口。该界面将具有外部人员可能需要完成其工作的方法。

So, each module will have an interface. This interface will have the methods that people from outside might need to do their job. Everything else is hidden from them.

创建模块A后,如果需要使用模块B的功能,则可以执行之类的操作。 setModuleBHandler(moduleB)

When module A is created, if it needs to use features of module B, you'll do something like setModuleBHandler(moduleB).

这样,您可以更改执行所需功能的模块,而调用者无需注意(因为接口仍然很荣幸)。

This way, you can change the modules that do the things you need without the caller noticing it (because the interface is still honored).

这将允许您遵循其他良好做法,例如TDD。

This will allow you to follow other good practices such as TDD.

示例:

假设模块Broker需要记录日志(并且您有一个专门用于记录日志的模块)。与其声明 public void setLogger(Logger logger),不如这样声明: public void setLogger(LoggerInterface logger)

Suppose module Broker needs logging (and you have a module dedicated to logging). Instead of declaring public void setLogger(Logger logger), prefer to declare it as follows: public void setLogger(LoggerInterface logger).

这几乎是相同的。以前起作用的仍然可以起作用。区别在于,现在您可以使用某些完全不同的Logger实现,而不必担心会影响使用它们的各种模块的影响。

This is almost the same. What worked before will still work. The difference is that now you can use some completely different Logger implementation without having to worry about impacts in the myriad of modules that can be using them.

例如,您可以有一个实现接口的TextLogger类和另一个实现相同接口的DBLogger。您可以互换它们而不会遇到任何问题。

For instance, you can have a class TextLogger implementing the interface and another DBLogger implementing the same interface. You can interchange them without expecting any problems.

注意:使用抽象类将获得几乎相同的结果。

Note: using an abstract class will get you nearly the same results.

我知道这个小例子不能正确显示此选择的全部优点,很多人都可以找到其中的缺点,但是我坚信这是大多数情况下的最佳选择(当然每个例子情况就是这样)。

I am aware that this small example doesn't properly show the full advantages of this choice and many people will be able to find drawbacks in it, but I strongly believe that this is the best choice for most cases (of course each case is a case).

这篇关于C ++:是否应该使用全局变量或类成员指针在模块之间进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆