Where do I use delegates?
哪些是现实世界中需要代表的地方?我很好奇在这种方法是最佳解决方案的情况下,会出现什么情况或模式。不需要代码。
如"学习C 3.0:掌握C 3.0的基本原理"所述。
General Scenario: When a head of state dies, the President of the United States typically does not have time to attend the funeral
personally. Instead, he dispatches a delegate. Often this delegate is
the Vice President, but sometimes the VP is unavailable and the
President must send someone else, such as the Secretary of State or
even the First Lady. He does not want to"hardwire" his delegated
authority to a single person; he might delegate this responsibility to
anyone who is able to execute the correct international protocol.The President defines in advance what responsibility will be delegated
(attend the funeral), what parameters will be passed (condolences,
kind words), and what value he hopes to get back (good will). He then
assigns a particular person to that delegated responsibility at
"runtime" as the course of his presidency progresses.In programming Scenario: You are often faced with situations where you need to execute a particular action, but you don’t know in
advance which method, or even which object, you’ll want to call upon
to execute it.For Example: A button might not know which object or objects need to be notified. Rather than wiring the button to a particular
object, you will connect the button to a delegate and then resolve
that delegate to a particular method when the program executes.
A delegate is a named type that defines a particular kind of method. Just as a class definition lays out all the members for the given kind of object it defines, the delegate lays out the method signature for the kind of method it defines.
基于此语句,委托是一个函数指针,它定义了该函数的外观。
谓词是委托的实际应用程序的一个很好的例子。在链接中的示例中,您将注意到数组。find使用数组进行搜索,然后使用谓词处理要查找内容的条件。在本例中,它传递一个与谓词签名匹配的方法productgt10。
一般列表中的委托的一个常见用法是通过动作委托(或其匿名等价物)创建一行foreach操作:
1 | myList.Foreach( i => i.DoSomething()); |
我还发现谓词委托在搜索或修剪列表时非常有用:
1 2 | myList.FindAll( i => i.Name =="Bob"); myList.RemoveAll( i => i.Name =="Bob"); |
我知道你说过不需要代码,但我发现通过代码更容易表达它的有用性。:)
将事件绑定到事件处理程序通常是对委托的第一个介绍……您甚至可能不知道自己在使用它们,因为委托被包装在EventHandler类中。
我和你有同样的问题,去这个网站找答案。
显然,我没有更好地理解它,即使我略读了这个线程上的例子。
我发现现在我对代表们很有用:http://www.c-sharpcorner.com/uploadfile/thiagu304/passdata05172006234318pm/passdata.aspx
对于新用户来说,这似乎更明显,因为表单传递值比带有post/get(querystring)的ASP.NET网站复杂得多。
基本上,您定义了一个以"文本框文本"为参数的委托。
//Frim1
1 2 3 4 5 6 7 8 9 10 11 12 | // Class Property Definition public delegate void delPassData(TextBox text); // Click Handler private void btnSend_Click(object sender, System.EventArgs e) { Form2 frm= new Form2(); delPassData del=new delPassData(frm.funData); del(this.textBox1); frm.Show(); } |
//摘要:定义委托,实例化新的Form2类,将fundata()函数赋给委托,将文本框传递给委托。显示表单。
//Frim2
1 2 3 4 5 | public void passData(TextBox txtForm1) { label1.Text = txtForm1.Text; } |
//摘要:只需将textbox txtform1作为参数(在委托中定义),并将标签文本分配给textbox的文本。
我希望这能对代表们有所启发:)..
我有一个使用win32 python的项目。
由于各种原因,一些模块使用odbc.py访问数据库,而其他模块使用pyodbc.py。
当两种模块都需要使用某个函数时,出现了一个问题。它有一个作为参数传递给它的连接对象,但是它必须知道是使用dbi.dbidate还是datetime来表示时间。
这是因为作为SQL语句中的值,odbc.py预期日期为dbi.dbidate,而pyodbc.py预期日期时间值。
另一个复杂的问题是,由odbc.py和pyodbc.py创建的连接对象不允许设置其他字段。
我的解决方案是用委托类包装由odbc.odbc(…)和pyodbc.pyodbc(…)返回的连接对象,委托类包含所需的时间表示函数作为额外字段的值,并将所有其他字段请求委托给原始连接对象。
如果您有兴趣了解委托模式在现实代码中的使用方式,请只看Mac OS X上的Cocoa。Cocoa是苹果公司在Mac OS X下编程的首选UI工具包,并用目标C编码。它的设计是为了通过委托而不是子类化或其他方式扩展每个UI组件。
有关更多信息,我建议查看苹果公司对这里代表的看法。
谷歌快速搜索出了这个http://en.wikipedia.org/wiki/delegation_模式。基本上,只要您使用一个对象将其调用转发给另一个对象,那么您就是在委派。