
Class TestPatterns
'<<1.Self-Defining Functions-自定义函数>>
Public Shared Sub SelfDefining()
Console.WriteLine(":: Pattern: Self-definining function")
Dim foo As Action = Sub()
Console.WriteLine("Hi there!")
foo = Sub()
Console.WriteLine("Hi again!")
End Sub
End Sub
Console.WriteLine("First call (initilization).")
foo()
Console.WriteLine("Second call - use different one now!")
foo()
Console.WriteLine("Third call - still the same.")
foo()
End Sub
'<<2.Callback Pattern-回调模式>>
Public Shared Sub Callback()
Console.WriteLine(":: Pattern: Callback pattern")
Console.WriteLine("Calling the function with lambda expression.")
CallMe(Function() "The boss.")
Console.WriteLine("Back at the starting point.")
End Sub
Private Shared Sub CallMe(ByVal caller As Func(Of String))
Console.WriteLine("Received function as parameter - Who called?!")
Console.WriteLine(caller())
End Sub
'<<3.Returning Functions-函数作为返回值>>
Public Shared Sub Returning()
Console.WriteLine(":: Pattern: Returning function")
Console.WriteLine("Calling to obtain the method ...")
Dim method As Func(Of Double, Double) = GetProperMethod("sin")
Console.WriteLine("Doing something with the method ...")
Console.WriteLine("f(pi / 4) = {0}", method(Math.PI / 4))
End Sub
Private Shared Function GetProperMethod(ByVal what As String) As Func(Of Double, Double)
Select Case what
Case "sin"
Return AddressOf Math.Sin
Case "cos"
Return AddressOf Math.Cos
Case "exp"
Return AddressOf Math.Exp
Case Else
Return Function(x) x
End Select
End Function
'<<4.Immediately-Invoked Function Expression-立即调用的函数表达式>>
Public Shared Sub IIFE()
Console.WriteLine(":: Pattern: IIFE")
DirectCast(Sub(x)
Console.WriteLine(2.0 * x * x - 0.5 * x)
End Sub, Action(Of Double))(1.0)
DirectCast(Sub(x, y)
Console.WriteLine(2.0 * x * y - 1.5 * x)
End Sub, Action(Of Double, Double))(2.0, 3.0)
End Sub
'<<5.Immediate Object Initialization-对象即时初始化>>
Public Shared Sub ImmediateObject()
Console.WriteLine(":: Pattern: Immediate object initialization")
Dim terminator = New With { _
Key .Typ = "T1000", _
Key .Health = 100, _
Key .Hit = DirectCast(Function(x)
Return 100.0 * Math.Exp(-x)
End Function, Func(Of Double, Double)) _
}
Console.WriteLine("Terminator with type {0} has been created.", terminator.Typ)
Console.WriteLine("Let's hit the terminator with 0.5. Rest health would be {0}!", terminator.Hit(0.5))
End Sub
'<<6.Init-Time Branching-初始化时间分支>>
Public Shared Sub InitTimeBranching()
Console.WriteLine(":: Pattern: Init-time branching")
Dim loopBody As Action(Of Integer) = Nothing
Console.WriteLine("Select a proper loop body method ...")
Dim r As New Random()
Dim sum As Integer = 0
If r.NextDouble() < 0.5 Then
Console.WriteLine("Selected random choice ...")
loopBody = Sub(index)
sum += r.[Next](0, 10000)
End Sub
Else
Console.WriteLine("Selected little gauss ...")
loopBody = Sub(index)
sum += index
End Sub
End If
Console.WriteLine("Execute the loop ...")
For i As Integer = 0 To 9999
loopBody(i)
Next
Console.WriteLine("Loop has finished with result sum = {0}.", sum)
End Sub
End Class

引用于:http://www.cnblogs.com/gaochundong/p/lambda_design_patterns.html#immediately_invoked_function_expression_pattern
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通