一,說明Hello這類別就是用來繼承MarshalByRefObject類別,在AOP中表示Pointcut。
Hello.vb
Public Class Hello Inherits MarshalByRefObject End Class
二,說明IHello這類別就是定義一個Interface,在AOP中表示Weave。
IHello.vb
Public Interface IHello Sub hello(ByVal name As String) End Interface
三,說明LogHandler這類別就是用來做動態Proxy Pattern,繼承RealProxy類別,在AOP中表示Advices。
LogHandler.vb
Imports System.Runtime.Remoting.Proxies Imports System.Runtime.Remoting.Messaging Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Services Public Class LogHandler Inherits RealProxy Private myMarshalByRefObject As MarshalByRefObject Public Sub New(ByVal myType As Type) MyBase.New(myType) myMarshalByRefObject = CType(Activator.CreateInstance(myType), MarshalByRefObject) End Sub Public Overrides Function Invoke(ByVal myIMessage As IMessage) As IMessage Dim calls As IMethodCallMessage = myIMessage Dim back As IMethodReturnMessage = Nothing Console.WriteLine("呼叫方法的名稱:" + calls.MethodName) back = RemotingServices.ExecuteMessage(myMarshalByRefObject, calls) Console.WriteLine("顯示的結果:" + back.ReturnValue.ToString()) Return back End Function End Class
四,說明HelloSpeaker這類別就是用來這類別就是用來繼承Hello類別並實作IHello介面,在AOP中表示Joinpoint。
HelloSpeaker.vb
Public Class HelloSpeaker Inherits Hello Implements IHello Public Sub hello(name As String) Implements IHello.hello Console.WriteLine("Hello:" + name) End Sub End Class
五,執行
ProxyDemo.vb
Module ProxyDemo Sub Main() Dim proxy As LogHandler = New LogHandler(New HelloSpeaker().GetType()) Dim Hello As IHello = proxy.GetTransparentProxy() Hello.hello("123") Console.ReadKey() End Sub End Module
沒有留言:
張貼留言