创建自定义协议和协议的相对优势Objective-c 中的代表
我想知道与实现双向类通信的其他技术相比,使用自定义协议和委托的相对优势是什么?
例如,另一个解决方案是具有以下内容:
A 关联到 BB 关联到 A
这样 A 和 B 都可以访问彼此的信息...
我有点理解协议允许增加系统的模块化性设计,但我不完全确定为什么或如何?
I was wondering what the relative advantages of using custom protocols and delegates are when compared to other techniques for achieving bi-directional class communication?
Another solution for example is to have the following:
A associated to B
B associated to A
This way both A and B have access to each others information...
I kind of get an understanding that protocols allow for an increase in the modularity of the system design, but I'm not entirely sure why or how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这种双向依赖是要避免的,因为在编译级别,这意味着每个包含都包含另一个包含。
当面向对象编程的目标之一是减少组件耦合时,您就将类绑定得太多了。
即使你能做到,也不意味着这是好的做法。
好的做法是指定 2 个协议,一种用于服务器/对象/生产者/...,一种用于客户端/委托/消费者/...
然后 A 将实现一个协议,与实现第二个协议的对象通信。
B 将实施第二个协议。
最后,这意味着您将来可以替换 B 实现以匹配新的 API/编程模型/测试存根等。
减少耦合是帮助代码模块化和可测试性的一种手段。
您不必这么做,但 6 个月后再次使用您的代码时您会很高兴:)
This kind of bi-directional dependency is to avoid as at compilation level it mean that you have each include including the other's.
You are tying your classes too much when one the goal of OO programming is to reduce component coupling.
Even if you can do it doesn't mean it's good practice.
Good pratice would be to specify 2 protocols, one for for the server/object/producer/... and one for the client/delegate/consumer/...
Then A would implement one protocol, would talk to object implementing the second one.
B would implement second protocol.
In the end that mean you can replace B implementation in the future to match a new API/programming model/test-stub, etc.
Reduce coupling is a mean to help modulartity and testability of your code.
You don't have to, but you'll be happy when coming back on your code 6 month later from now :)
通常两个对象不需要进行完全的双向通信。
通常在 iPhone 中,您从 A 启动 B(例如,从列表视图控制器启动详细视图控制器)并将 A 设置为 B 的委托,以便它将收到相关事件的直接通知。
就面向对象设计/编程而言,以完全双向连接方式连接 2 个对象是不正确的。
在这种情况下,您会松开封装。
一旦您使用委托设计模式,您的对象并不真正相互了解,但仍然可以通信。
此外,通过这种方式,任何实现特定协议的对象都可以设置为委托。
这样,对象也不必相互保留......
Usually there is no need for 2 objects to have a full bi-directional communication.
Usually in iPhone you initiate B from A (e.g. detailed view controller from a list view controller) and set A to be B's delegate so that it will receive direct notification on relevant events.
It is not right in terms of object oriented design/programming to connect 2 objects in a fully bi-directional connection.
You loose the encapsulation in this case.
Once you use the delegation design pattern your object don't really know each other but still can communicate.
In addition, this way any object that implements a certain protocol may be set as a delegate.
This way the objects also don't have to retain each other...
自定义委托协议是一件很棒的事情,它允许您的对象不依赖于特定的类。任何符合给定协议的对象都可以作为委托。例如,任何实现 NSTableViewDelegate 协议的对象都可以成为表视图的委托。
否则,如果使用直接关联,则必须使用某个类的对象。
Custom delegate protocol is a great thing, it allows your object to not depend on the particular class. Any object that conforms to the given protocol can be the delegate. For example, any object can be the delegate for the table view if it implements NSTableViewDelegate protocol.
Otherwise, if you use direct association, you have to use object of the certain class.
通过委托模式(特别是协议的使用),类保持松散耦合。在考虑 MVC 模式时,这一点很重要。委托模式允许视图与控制器保持解耦。
另外,“A关联到BB关联到A”将创建一个保留周期。委托模式对内存管理问题进行了编码(即类不应保留其委托)。
With the delegate pattern (specifically the use of a protocol) the classes stay loosely coupled. This is significant when considering the MVC pattern. Delegate pattern allows the view to stay decoupled from the controller.
Also, "A associated to B B associated to A" would create a retain cycle. The delegate pattern codifies the memory management issues (i.e. a class should not retain its delegate).