委托引用其为委托的对象是不是一种坏的做法?

I'm using the delegate pattern for one of my objects. My idea is that I will be able to swap the delegate out later for a different delegate implementing a different strategy. I suppose this is just as much the strategy pattern as the delegate pattern.

My question is, is it bad practice for my delegate to have a reference back to the object for which it is the delegate? There are a couple properties of that object that I need to have access to regardless of which delegate/strategy I use. If this is bad form, how should I get access to the properties?

#0

Not necessarily. For example, it's quite common for a "controller"-type object to create data objects and views and make itself the delegate of some of those objects in order to properly control things.

#1

It is not at all bad practice. The delegate pattern allows for a class to have a generic way to talk to any number of objects as long as the implement the same protocol. But the class on which you set the delegate also will usually have a number of public properties or methods that allow you to query or alter what the class is doing, in response to which the class may in turn trigger a number of the delegate calls. So you need a reference of the class to which you are the delegate, in order to tell the object to do something different than it is already, and of course to release it when you are through with it!

This is also why it's important to always have any delegate properties as assign, and not retain, properties. That way when the originating class is released it will actually be released, instead of having delegate objects it is holding onto causing a retain loop that keeps both around.

It's also why whenever you are being released, you should set the delegate reference to nil in anything you may have a delegate reference set. This way a class will not have an invalid delegate reference if the delegate is released before the class that uses a delegate.

#2

Generally a dependency should not have a reference back to the dependent object, as this would be a classical circle reference. To avoid having a back-reference you can supply the needed properties as arguments in a delegate method, or you move the properties themselves into the delegate.

推荐文章

知道在Windows计算机上要杀死哪个java.exe进程

知道在Windows计算机上要杀死哪个java.exe进程

推荐文章

如何更新大型XML文件

如何更新大型XML文件

推荐文章

拒绝所有输入的正则表达式?

拒绝所有输入的正则表达式?

推荐文章

如何用Java编写Facebook应用程序?

如何用Java编写Facebook应用程序?

推荐文章

DevExpress eXpressApp框架(XAF)和eXpress持久对象(XPO):如何加快关联的加载时间?

DevExpress eXpressApp框架(XAF)和eXpress持久对象(XPO):如何加快关联的加载时间?

推荐文章

如何防止Flash的URLRequest转义url?

如何防止Flash的URLRequest转义url?

推荐文章

tomcat5无法在CentOS 5上启动,并带有NoClassDefFoundError异常

tomcat5无法在CentOS 5上启动,并带有NoClassDefFoundError异常

推荐文章

如何在.NET中的线程上传播tcplistener传入连接?

如何在.NET中的线程上传播tcplistener传入连接?

推荐文章

任何支持5.3语法的PHP编辑器?

任何支持5.3语法的PHP编辑器?

推荐文章

如何获得Axis 1.4而不为同一XML名称空间生成多个前缀?

如何获得Axis 1.4而不为同一XML名称空间生成多个前缀?

推荐文章

如何使用Eclipse为诺基亚创建J2ME Midlet

如何使用Eclipse为诺基亚创建J2ME Midlet

推荐文章

线串之间的相似性

线串之间的相似性

推荐文章

远程安装Windows服务

远程安装Windows服务

推荐文章

我应该使用int还是Int32

我应该使用int还是Int32

推荐文章

有什么方法可以在ms-access查询中创建多个insert语句?

有什么方法可以在ms-access查询中创建多个insert语句?

推荐文章

如何在Subversion中移动标签

如何在Subversion中移动标签