1. 程式人生 > >靜態代理與動態代理模式

靜態代理與動態代理模式

png 代理 before ring program 直接 factor 設計模式 messaging

代理(Proxy)設計模式簡介:

提供了對目標對象另外的訪問方式;即通過代理對象訪問目標對象.這樣做的好處是:可以在目標對象實現的基礎上,增強額外的功能操作,即擴展目標對象的功能.
這裏使用到編程中的一個思想:不要隨意去修改別人已經寫好的代碼或者方法,如果需改修改,可以通過代理的方式來擴展該方法
舉個例子來說明代理的作用:假設我們想邀請一位明星,那麽並不是直接連接明星,而是聯系明星的經紀人,來達到同樣的目的.明星就是一個目標對象,他只要負責活動中的節目,而其他瑣碎的事情就交給他的代理人(經紀人)來解決.這就是代理思想在現實中的一個例子

技術分享圖片

靜態代理:

靜態代理在使用時,需要定義接口或者父類,被代理對象與代理對象一起實現相同的接口或者是繼承相同父類.

    class Program
    {
        static void Main(string[] args)
        {
            Member member = new Member() { Id=1,Name="test member"};
            IMemberService baseMemberService = new MemberServiceImpl();

            //代理原有業務類
            MemberSerivceImplStaticProxy memberService = new
MemberSerivceImplStaticProxy(baseMemberService); //遵守接口契約 string result = memberService.Create(member); System.Console.WriteLine(result); System.Console.ReadKey(); } } class Member { public int Id { get; set; }
public String Name { get; set; } public override string ToString(){ return string.Format("Member[Id={0},Name={1}]", this.Id, this.Name); } } interface IMemberService { string Create(Member member); } class MemberServiceImpl : IMemberService { public string Create(Member member){ Console.WriteLine("create member:"+member); return "SUCCESS"; } } class MemberSerivceImplStaticProxy { private IMemberService target; public MemberSerivceImplStaticProxy(IMemberService target) { this.target = target; } public string Create(Member member){ before(member); string result= target.Create(member); after(member); return result; } private void before(Member member){ Console.WriteLine("before " + member); } private void after(Member member){ Console.WriteLine("after " + member); } }

動態代理:

動態代理有以下特點:
1.代理對象,不需要實現接口
2.代理對象的生成,是利用JDK的API,動態的在內存中構建代理對象(需要我們指定創建代理對象/目標對象實現的接口的類型)
3.動態代理也叫做:JDK代理,接口代理

    class Program
    {
        static void Main(string[] args)
        {
            Member member = new Member() { Id=1,Name="test member"};
            IMemberService baseMemberService = new MemberServiceImpl();
            
            //代理原有業務類
            IMemberService memberService = (IMemberService)new MemberServiceImplDynamicProxy(baseMemberService).GetTransparentProxy();
            //遵守接口契約
            string result = memberService.Create(member);

            System.Console.WriteLine(result);
            System.Console.ReadKey();
        }
    }

    class Member {
        public int Id { get; set; }
        public String Name { get; set; }

        public override string ToString(){
            return string.Format("Member[Id={0},Name={1}]", this.Id, this.Name);
        }
    }

    interface IMemberService {
        string Create(Member member);
    }

    class MemberServiceImpl : IMemberService {
        public string Create(Member member){
            Console.WriteLine("create member:"+member);
            return "SUCCESS";
        }
    }

    class MemberServiceImplDynamicProxy : System.Runtime.Remoting.Proxies.RealProxy {
        private IMemberService target;

        public MemberServiceImplDynamicProxy(IMemberService target) : base(typeof(IMemberService)){
            this.target = target;
        }

        public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg){
            System.Runtime.Remoting.Messaging.IMethodCallMessage callMessage = (System.Runtime.Remoting.Messaging.IMethodCallMessage)msg;
            Member member = callMessage.Args[0] as Member;

            before(member);
            object returnValue = callMessage.MethodBase.Invoke(this.target, callMessage.Args);
            after(member);

            return new System.Runtime.Remoting.Messaging.ReturnMessage(returnValue, new object[0], 0, null, callMessage);
        }

        private void before(Member member){
            Console.WriteLine("before " + member);
        }

        private void after(Member member){
            Console.WriteLine("after " + member);
        }
    }

為了讓動態代理類更通用,可以對其調整為如下:

    class DynamicProxyFactory : System.Runtime.Remoting.Proxies.RealProxy
    {
        private object target;

        public DynamicProxyFactory(object target, Type targetType)
            : base(targetType)
        {
            this.target = target;
        }

        public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
        {
            System.Runtime.Remoting.Messaging.IMethodCallMessage callMessage = (System.Runtime.Remoting.Messaging.IMethodCallMessage)msg;

            before(this.target);
            object returnValue = callMessage.MethodBase.Invoke(this.target, callMessage.Args);
            after(this.target);

            return new System.Runtime.Remoting.Messaging.ReturnMessage(returnValue, new object[0], 0, null, callMessage);
        }

        private void before(object member)
        {
            Console.WriteLine("before " + member);
        }

        private void after(object member)
        {
            Console.WriteLine("after " + member);
        }
    }

對應客戶端調用時也要變,變為如下:

    class Program
    {
        static void Main(string[] args)
        {
            Member member = new Member() { Id = 1, Name = "test member" };
            IMemberService baseMemberService = new MemberServiceImpl();

            //代理原有業務類
            IMemberService memberService = (IMemberService)new DynamicProxyFactory(baseMemberService, typeof(IMemberService)).GetTransparentProxy();
            //遵守接口契約
            string result = memberService.Create(member);

            System.Console.WriteLine(result);
            System.Console.ReadKey();
        }
    }

靜態代理與動態代理模式