Communicate between objects
2013-02-28 20:30
141 查看
By Matthijs
When you have two objects A and B, says two view controllers, that you want to make talk to each other, you can choose from the following options:
NSNotifiationCenter.This is anonymous one-to-many communication. Objects A posts a notification to the NSNotificationCenter, which then distributes it to any other objects listening for that
notification, including Object B. A and B do not have to know anything about each other, so this is a very loose coupling.
KOV (Key-Value Observing).One object observes the properties of another. This is a very tight coupling, because Objects B is now peeking directly into Object A. The advantage of KVO is that
Object A doesn't have to be aware of this at all, and therefore does not need to send out any notifications -- the KVO mechanism takes care of this behind the scenes.
Direct Pointers.
Object A has a pointer to Object B and directly sends it messages when something of interest happens. This is the tightest coupling possible because A and B cannot function without each other. In the case of view controllers you generally want to avoid this.
Delegates. Object B is a delegate of Object A. In this scenario, Object A does not know anything about Object B. It just knows that some object performs the role of its delegate and it will
happily send messages to that delegate, but it doesn't know -- or care -- that this is Object B. The delegate pattern is often the preferred way to communicate between view controllers, but it takes some work to set up.
Blocks. Essentially the same approach as delegates, except that Object B now gives Object A one or more blocks(closures) to be executed when certain events take place. There is no formal delegates
protocol and the only thing that Object A sees of Object B is the blocks it is given.
When you have two objects A and B, says two view controllers, that you want to make talk to each other, you can choose from the following options:
NSNotifiationCenter.This is anonymous one-to-many communication. Objects A posts a notification to the NSNotificationCenter, which then distributes it to any other objects listening for that
notification, including Object B. A and B do not have to know anything about each other, so this is a very loose coupling.
KOV (Key-Value Observing).One object observes the properties of another. This is a very tight coupling, because Objects B is now peeking directly into Object A. The advantage of KVO is that
Object A doesn't have to be aware of this at all, and therefore does not need to send out any notifications -- the KVO mechanism takes care of this behind the scenes.
Direct Pointers.
Object A has a pointer to Object B and directly sends it messages when something of interest happens. This is the tightest coupling possible because A and B cannot function without each other. In the case of view controllers you generally want to avoid this.
Delegates. Object B is a delegate of Object A. In this scenario, Object A does not know anything about Object B. It just knows that some object performs the role of its delegate and it will
happily send messages to that delegate, but it doesn't know -- or care -- that this is Object B. The delegate pattern is often the preferred way to communicate between view controllers, but it takes some work to set up.
Blocks. Essentially the same approach as delegates, except that Object B now gives Object A one or more blocks(closures) to be executed when certain events take place. There is no formal delegates
protocol and the only thing that Object A sees of Object B is the blocks it is given.
相关文章推荐
- Number of days between two Date objects (Ruby)
- How to copy all view private files and Derived objects between views
- Interaction Between Threads and Securable Objects
- The relationship between Objects and Closures
- The Truth About .NET Objects And Sharing Them Between AppDomains
- 改善代码设计 —— “.NET研究”优化物件之间的特性(Moving Features Between Objects)
- 重构—改善既有代码的设计007:在对象之间搬移特性(Moving Features Between Objects)
- 一起谈.NET技术,改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects)
- python multiprocess.Queue - RuntimeError: Queue objects should only be shared between processes through inheritance
- [转]Similarities between Hibernate and JDBC objects
- How to: Use Named Pipes to Communicate Between Processes over a Network
- confilicts between permissions on a store procedure and the objects it referers to
- Image stabilization in vertebrates depends on contrast between objects
- Define a one-to-many dependency between objects so that when one object changes state, all its depe
- 在对象之间搬移特性(Moving features Between Objects)
- 改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects)
- 改善代码设计 —— 优“.NET技术”化物件之间的特性(Moving Features Between Objects)
- 【python】RuntimeError: Lock objects should only be shared between processes through inheritance
- Interaction between objects
- Difference Between Crystal Reports and Business Objects