配置对象时DataSource模式与设置属性 [英] DataSource pattern versus setting Properties while Configuring Objects

查看:196
本文介绍了配置对象时DataSource模式与设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常对何时使用DataSource Pattern以及何时使用Properties为对象提供配置信息感到困惑。

I often get confused with when to use DataSource Pattern and when to use the Properties for providing configuration information to objects.

我有两种方法可以做到这一点,

I have two ways to do this,

通常我在Object的类中保留了许多必须配置的属性,以及一个重置对象并继续使用新属性的方法。

Generally I keep a lot of properties in the Object's class that has to be configured and a method that resets the object and continues with the new properties.

对于配置另一个对象的Object,我保留了一个名为configureXYZ:WithValues:的方法,它重置属性并调用要配置的对象的reset方法。

And the for the Object which is configuring the other object, I keep a method that with the name configureXYZ:WithValues: , which resets the properties and calls the reset method of the object to be configured.

我在MPMoviePlayerController中看到,我们必须设置属性。

This I have seen with MPMoviePlayerController, that we have to set properties.

其他方法是如何使用tableView有效,所有配置信息都来自数据源方法。

and Other way is how tableView works, all the configuration information comes from datasource methods.

在任何情况下,任何人都可以更多地了解首选方式。

Can anyone throw more light on which way is preferred in which scenario.

因为我常常想要使用设计模式并制作代码看起来时尚,但我想知道我们什么时候才真正需要这些。
我对委托模式绝对清楚,并且必须定期使用它。
DataSource是我从未明确的一件事。

Because Its often I feel tempted to use design patterns and make the code look stylish but I wanted to know when do we actually need these. I am absolutely clear with delegate pattern and have to use it on regular basis. DataSource was one thing I was never clear with.

推荐答案

在设计课程时,你应该考虑的关键因素在决定使用委托或属性时,值的变化频率。如果您将值设置一次并且它们永远不会再次更改,则属性最有效。代表(其中数据源只是一个示例)如果值可能随时间变化或因条件而发生变化,则效果最佳。

When designing a class, the key factor you should consider when deciding between using a delegate or properties is how often the values can change. Properties work best if you will set the values one time and they should never change again. Delegates (of which datasource is just an example) work best if the values might change over time or change due to conditions.

例如,在中UITableView ,行数高度动态。它可能会因为表视图控制之外的多种原因而发生变化。行甚至代表的是高度动态的。它们可能是数据;它们可能是菜单选项;他们可能是游戏中的碎片。 UITableView 不会尝试猜测或控制其中的任何内容。它将它移动到一个委托(数据源),在那里可能做出非常复杂的决定。

For example, in UITableView, the number of rows is highly dynamic. It could change for many reasons outside of the control of the table view. What the rows even represent is highly dynamic. They might be data; they might be menu options; they might be pieces in a game. UITableView doesn't try to guess or control any of that. It moves it to a delegate (datasource) where potentially very complex decisions could be made.

MPMoviePlayerController 有一些控制意味着非常具体的东西,几乎永远不会改变(特别是一旦电影开始播放)。基本上你设置的东西,点击播放然后走开。在这种情况下,代表可能会有点矫枉过正。

MPMoviePlayerController has a few controls that mean very specific things and should almost never change (particularly once the movie starts playing). Basically you set the thing up, hit play and walk away. In that case, a delegate would likely be overkill.

有很多案例处于中间位置,无论哪种方式都可以。我鼓励开发人员首先考虑委托,然后如果没有意义,请考虑属性。这不是因为委托总是正确的答案,而是因为大多数C ++ - 或受过Java教育的开发人员不会考虑授权,所以应该有意识地这样做。

There are many cases that are in the middle, and either way may be ok. I would encourage developers to consider delegation first, and then if it doesn't make sense go with properties. This isn't because delegation is always the right answer, but more because most C++- or Java-educated developers don't think in terms of delegation, so should make a conscious effort to do so.

其他一些想法如下:


  • 使用属性时,如果配置它们是理想的在初始化时,之后是不可变的。这解决了很多问题。

  • When using properties, it is ideal if they are configured at initialization time and are thereafter immutable. This solves a great number of problems.

如果你发现自己需要很多属性,委托可能会更好,而且往往更简单。

If you find yourself needing a lot of properties, delegation is probably better and often simpler.

委托通知方法( somethingDidHappen:)通常更好地实现为块。 (块在ObjC中相对较新。许多基于委托的Apple接口正在转向块,但出于历史原因,你会看到真正的混合。)

Delegate notification methods (somethingDidHappen:) are often better implemented as blocks. (Blocks are relatively new in ObjC. Many delegate-based Apple interfaces are moving to blocks, but you'll see a real mix out there for historical reasons.)

委托和数据源之间的区别在于委托管理行为,而数据源则提供数据。它们通常以相同的方式实现。

The difference between "delegate" and "datasource" is that a delegate manages behavior, while a datasource provides data. They are typically implemented identically.

这篇关于配置对象时DataSource模式与设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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