可可触摸连接和数据设计模式 [英] Cocoa touch connection and data design pattern

查看:128
本文介绍了可可触摸连接和数据设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是一个设计问题,它在过去几天一直驾驶疯狂。我是可可触摸发展的新手。

My question is a design issue and it has been driving crazy over the last couple of days. I am new to cocoa touch development.

我有一个应用程序有一个UINavigarion控制器和3个意见。我需要保持与WCF服务的通信并将数据存储在应用程序端。

I have an application that has a UINavigarion controller and a 3 views. I need to keep communicate with a WCF service and store the data on the app side.

如何创建我的模型(MVC),使数据可供所有控制器使用?

How do I create my Model (MVC) in a way that makes the data available to all controllers?

我开始做的是一个单例,它处理所有的存储和网络调用,但我通过线程读到这是一个坏主意。我也考虑把代码放在appDelegate,但是人们说这也是个坏主意。

What I started doing is a singleton that handles all the storage and web calls but I read thru the threads that it is a bad idea. I also considered putting the code in the appDelegate but people say that is a bad idea too.

从概念上讲,如何设计模型并与控制器沟通?

Conceptually how would u design your model and communicate with the controllers?

任何帮助都非常感谢。

Any help is much appreciated.

推荐答案

你会得到不同的建议,因为意见不同的最佳使用方法。正如你所指出的,对于处理数据模型,有两个主要的设计模式:依赖注入和单例。

You will get different advice because opinions differ on the best method to use. As you noted, for handling a data model, there are two major design patterns: Dependency Injection and Singleton.

依赖注入依赖于根据需要将数据模型对象从视图控制器传递到视图控制器。 Marcus Zarra(作者核心数据:Apple的API,用于在Mac OS X上持久化数据我推荐)写了一篇解释依赖注入的好文章。大多数Apple文档建议您使用依赖注入设计。

Dependency Injection relies on passing the data model object from view controller to view controller as needed. Marcus Zarra (author of Core Data: Apple's API for Persisting Data on Mac OS X which I recommend) wrote a good article explaining Dependency Injection. Most of the Apple documentation recommends that you use the Dependency Injection design.

我喜欢Singleton模式,但对于新手来说非常非常危险。 Singleton模式是很容易犯错误,大多数灰狗已放弃它,只是建议新手永远不要使用它。

I like the Singleton pattern but it is very, very, very dangerous for a novice to use. The Singleton pattern is so easy to do wrong that most graybeards have given up on it and simply advise novices to never use it.

Singleton模式具有提高应用程序的模块性和灵活性的优点。但是,它要求您在开始编码数据模型和UI之前,确定数据模型将做什么。它需要更多的工作前面得到正确,它不是作为依赖注入原谅。你必须使用更大规模的单身。

The Singleton pattern has the advantage of increasing modularity and flexibility of the app. However, it requires that you have a firm idea of what the data model will do before you start coding both the Data Model and the UI. It requires more work up front to get correct and it is not as forgiving as Dependency Injection. You have to use singletons with greater discipline.

对于具有三个层次视图的简单应用程序,依赖注入是最简单,最干净的实现设计。你不需要单例的灵活性,单例只会增加不必要的复杂性。基于导航的模板与核心数据提供的Xcode将给你50%的应用程序启动。只需添加第二和第三层视图,你就完成了。

For a simple app with three hierarchal views, Dependency Injection is the simplest and cleanest design to implement. You won't need the flexibility of a singleton and a singleton will just add unneeded complexity. The Navigation-based template with Core Data provided by Xcode will give you 50% of the app to start. Just add the second and third tier views and you're done.

这篇关于可可触摸连接和数据设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆