实现 MVC 的模型部分
我制作了一个简单的游戏,仍在研究游戏的一些附加部分。我不确定如何/在哪里存储我的游戏数据(分数、设置等)
我一直在使用单例,其中包含我将在合成的程序中使用的所有数据,以便于访问,但我不知道这是一个好方法吗?在我看来,这将使设置等变得非常容易,因为我可以在菜单视图和游戏视图之间共享单例,而无需复制数据..还有其他方法吗?到目前为止,我读过的每一本书似乎都跳过了模型,只是将其与 MVC 的控制器部分结合起来。
I have a simple game made, still working on some of the additional portions of the game. I am not sure how/where to store my game data (scores, settings, etc)
I've been using a singleton with all of the data that I'll use in the program synthesized for ease of access, but I don't know if this is a good method? It seems to me it would make settings and such quite easy, because I can just share the singleton among the menu view and the game view, without having to copy the data.. Is there another way? Every book I've read so far seems to skip over the model and just combine it with the controller portion of the MVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单例虽然因其简单性而吸引人,但通常会使代码库对更改的灵活性降低。他们也不鼓励在模型中明确分离功能。
另一种方法是使用模型的核心数据。您将拥有一个用于游戏、分数、设置等的托管对象,并传递对对象的引用。借助 Core Data,您可以免费获得持久性以及快速/高效的数据排序和呈现机制。
Singletons while appealing for their simplicity generally make the code base less flexible to changes. They also do not encourage a clear separation of functionality in the model.
An alternative is to use core data for the model. You would have a managed object for game,scores, settings etc and pass around references to the objects. With Core Data you get persistence for free along with fast/efficient mechanisms for sorting and presenting data.