模型视图控制器问题
我最近一直在开发我的iPhone游戏,在决定我的各个类别的设计时遇到了岔路。到目前为止,我一直遵循 MVC 模式,但以下情况让我感到困惑:
我有 4 个直观显示的按钮。每个按钮都包含一个容器 UIView(我对其进行了子类化)和 2 个 UIButtons(也对其进行了子类化)作为子视图。当您按下按钮时,它会执行翻转效果以及其他效果。用户输入使用目标操作从我的容器 UIView 到我的控制器。这部分没问题,以下部分是有争议的部分:
所以我已经对容器视图和 UIButtons 进行了子类化,并且我需要添加更多数据/方法(在某处)来执行更多操作。将需要序列化的数据和非渲染相关的代码放在视图类中似乎破坏了 MVC 设计,但目前,对我来说将其放在那里最有意义。这几乎就像我的子类视图是它们自己的小 MVC,而且看起来很整洁。在这种情况下,将数据/方法从视图中分离到我的主控制器似乎没有必要,而且需要更多工作。我应该怎样做呢?
谢谢大家。
I've been working on my iphone game recently and came across a forked road when deciding the design of my various classes. So far I've adhered to the MVC pattern but the following situation had me confused:
I have 4 buttons displayed visually. Each button though consists of a container UIView (which I've subclassed) and 2 UIButtons (also subclassed) as subviews. When you press a button, it does the flip effect plus other stuff. The user input is using target-action from my container UIView to my controller. This part is ok, the following part is the debatable part:
So I've subclassed the container view as well as the UIButtons and I need to add more data/methods (somewhere) to do more things. Putting data that needs to be serialized and non-rendering related code in the view classes seems to break the MVC design but at the moment, it makes the most sense to me to put it there. It's almost like my subclassed views are their own little MVC's and it seems neat. Separating out the data/methods from the view to my main controller in this case seems unnecessary and a more work. How should I be doing it?
Thanks heaps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MVC 模式非常有用,因为它允许您重用 MVC 模型的至少 2 个部分(通常是模型和视图),因此通常编写干净代码的最佳方法是避免使用继承并使用委托(基于协议)和依赖项注入(针对服务),以便您可以为系统生成单元测试以及升级代码开发的更好方法
以下是一些有趣的文章:
The MVC pattern is quite useful because it lets you reuse at least 2 parts of the MVC model (usually the model and the view) so usually, the best way to write clean code is avoid the use of inheritance and use instead delegation (based in protocols) and dependency injection (for the services) so you can generate unit tests for your systems and a better way to upgrade the develop of code
Here are some interesting articles: