如果并发不是问题,是否有理由避免使用全局对象?

发布于 2024-12-20 02:01:43 字数 408 浏览 1 评论 0原文

我正在开发一款 iPhone/iOS 游戏,我想知道我是否可以/应该使用全局对象来进行场景之间的统计跟踪。现在等等,在你跳下我的喉咙之前 - 是的,它违背了我所知道的关于好代码的一切,但我不认为并发在这种情况下会成为问题。我的游戏结构类似于《俄勒冈小道》(Oregon Trail)的游戏,其中加入了一些文明元素 - 因为它不是实时的,我可以确保一次只有一个进程会访问有问题的对象,并且因此我很确定并发根本不会成为问题。基本上每个场景都需要访问大部分数据(人口、当前技术水平、当前日期、当前地图状态),所以我想知道是否可以使用全局对象来存储这些数据。

我在大学里学到的一切都在尖叫着这是一个坏主意,但我内心偷偷摸摸的程序员部分却不断低声说:“它不会那么糟糕,只是这一次不会有什么伤害。”不用担心并发性,我有点倾向于偷偷摸摸的程序员。还有其他原因我应该避免使用全局对象进行这种统计跟踪吗?

I'm working on an iPhone/iOS game, and I was wondering if I could/should use a global object for statistic tracking between scenes. Now wait, before you jump down my throat - yes, it goes against everything I know about good code, but I don't think that concurrency is going to be an issue in this case. The structure of my game is similar to an Oregon Trail kind of affair with a sprinkling of Civilization thrown in for good measure - since it's not real-time I can ensure that only one process will be accessing the object in question at one time, and thus I'm pretty sure concurrency will not be an issue at all. Basically every scene will need access to most of the data (population, current technology level, current date, current map state) and so I'm wondering if I can get away with using a global object to store this data.

Everything I ever learned in college screams that this is a Bad Idea, but the sneaky programmer part of me keeps whispering "it won't be that bad, just this once couldn't hurt." And without worrying about concurrency, I'm kind of leaning towards the sneaky programmer. Is there any other reason I should avoid using a global object for this kind of statistic tracking?

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

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

发布评论

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

评论(1

咽泪装欢 2024-12-27 02:01:43

您能解释一下全局对象的含义吗?

  1. 如果您谈论的是单例设计模式,那么使用它并没有什么问题。
  2. 如果您正在谈论不同线程可以修改的对象实例,那么如果您小心并正确地在对象上使用 @synchronize ,那么这样做没有什么问题,
  3. 但我认为假设您的应用程序将是单线程的,这是错误的它的整个生命周期是因为 iOS 可以在幕后做一些事情,并且它会阻止你进行任何异步处理(我还假设任何游戏都需要一些重要的处理,并且此处理应该在后台线程上完成,以便让主线程尽可能响应关于用户交互)

Can you explain what you mean by global object?

  1. If you're talking about the singleton design pattern, there's nothing wrong using it.
  2. If you're talking about an object instance that different threads can modify, there's nothing wrong doing this, if you're careful and use @synchronize on objects correctly
  3. But I think it iS wrong to assume your application is going to be mono thread during its whole lifecycle for the reason that iOS can do stuff behind the scene and that it would prevent you to do any asynchronous processing (and I also assume any game requires some non-trivial processing and this processing should be done on a background thread to let the main thread be as responsive as possible on user interactions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文