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 SubEnd Class
引用于: