model view controller question
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 th开发者_高级运维e view to my main controller in this case seems unnecessary and a more work. How should I be doing it?
Thanks heaps.
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:
- when would I use the delegation pattern instead of inheritence to extend a class's behavior?
- Inheritance is evil, and must be destroyed
- Dependency Injection
精彩评论