禁用不支持的功能的好方法是什么?
我想这个问题与语言无关,尽管我问的是关于构建一个使用新游戏中心 API 的 iPhone 应用程序,但请随意用一般软件工程术语来回答。
我正在为 iPhone 构建一款游戏,该游戏利用了新的 Game Center 功能(即自动匹配、排行榜、成就等),但我想编写该游戏,以便它可以在所有 iPhone 上运行,包括那些未安装 Game Center,无法使用 Game Center 功能。为此,Apple 建议采用以下方法...
“我们建议制作一个应用程序版本,该版本可以动态检测 Game Center 是否可用,并据此使用(或不使用)它。”
水平,实现此目的的简单方法是检查 Game Center 是否可用并相应地设置一个简单的布尔标志。然后使用该标志来控制整个软件的执行流程。我确信我可以做到这一点,但因为我喜欢学习和编程,我想知道是否有更好的方法或设计模式来禁用不支持的功能块,并控制执行流程。
预先感谢您的智慧!
I suppose this question is language-agnostic, tho I'm asking it in regards to building an iPhone app that uses the new Game Center API, but please feel free to answer in general software engineering terms.
I'm building a game for the iPhone that takes advantage of the new Game Center capabilities (i.e. Auto-matching, leaderboards, achievements, etc.), but I want to write the game so that it works on all iPhones, including those that don't have Game Center installed and cannot make use of the Game Center capabilities. To do this, Apple recommends the approach...
"We'd recommend making one version of the app which dynamically detects whether Game Center is available and uses it (or not) based on that."
With my current level of programming, the simple approach I would take to implementing this would be to check if whether or not Game Center is available and set a simple boolean flag accordingly. Then use that flag to control the flow of execution throughout the software. I'm sure I could make that work, but because I enjoy learning and enjoy programming, I was wondering if there's a better approach or design pattern for disabling blocks of functionality that aren't supported, along with controlling the flow of execution.
Thanks in advance for your wisdom!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这些情况下通常使用的称为外观模式。在您的情况下,您将为应用程序中使用的游戏中心的功能构建一个包装器,然后进行两种实现 - 一种可能只不过是对游戏中心的代理调用,另一种则根据需要返回预设答案。
我要指出的是,我从未做过任何 iOS/objective C 编程,所以我不知道如何在该环境中正确地实现这一点。
What you generally use in these cases is called the Facade Pattern. In your case, you'd build a wrapper for the functions of the game center you use in your app, and then two implementations -- one which probably does little more than proxy calls to the game center and another which returns canned answers as required.
I will note that I've never done any iOS/objective C programming so I have no idea how one would actually implement this properly in that environment.
查看 Apple 开发网站上的 Game Kit 代码示例。他们实现了一个 GameCenterManager 类,该类可以很好地完成您想要完成的任务。
Check out the Game Kit code examples on Apple's dev site. They implement a GameCenterManager class that will work well for what you're trying to accomplish.
我通常使用简单的 C 风格函数来测试 Game Center 支持,该函数扩展了 Apple 推荐的方法。这增加了针对 iPod Touch 第一代和 3G 型号的设备测试,因为 Apple 的代码不考虑这些设备。
使用方法很简单
I typically test for Game Center support with a simple C-style function that extends Apple's recommended method. This adds a device test for iPod Touch 1st Gen and 3G models, since Apple's code does not account for those devices.
Usage is as simple as