代理模式

  1. 代理模式:  1)动态代理

                          2)静态代理

    2. 代理三要素:

            1)共同接口
            2)真实对象
            3)代理对象
       代理模式的优点:扩展原功能,不侵入原代码
       静态代理的实现:

            public interface Action {
                public void doSomething();
            }
            
            public class RealObject implements Action{
             
                public void doSomething() {
                    System.out.println("do something");
                }
            }

            public class Proxy implements Action {
                    private Action realObject;
                 
                    public Proxy(Action realObject) {
                        this.realObject = realObject;
                    }
                    public void doSomething() {
                        System.out.println("proxy do");
                        realObject.doSomething();
                    }
                }

            //调用:
             Proxy proxy = new Proxy(new RealObject());
             proxy.doSomething();

3. 场景:假如有这样一个需求,有3个不同的RealObject,同时我们要去代理的方法是不同的,比要代理方法:doSomething、doAnotherThing、doTwoAnotherThing,添加代理前,原代码可能是这样的:

    realObject.doSomething();
    realObject1.doAnotherThing();
    realObject2.doTwoAnother();

    实现: 
        方案1:为不同的方法创建不同的代理类
        1)为第一个方法创建代理类

            public interface Action {
                public void doSomething();
            }
            
            public class RealObject implements Action{
             
                public void doSomething() {
                    System.out.println("do something");
                }
            }

            public class Proxy implements Action {
                    private Action realObject;
                 
                    public Proxy(Action realObject) {
                        this.realObject = realObject;
                    }
                    public void doSomething() {
                        System.out.println("proxy do");
                        realObject.doSomething();
                    }
                }


        2)为第二个方法创建代理类    
      

 public interface Action2 {
                public void doAnotherThing();
            }
            
            public class RealObject2 implements Action2{
             
                public void doAnotherThing() {
                    System.out.println("do AnotherThing");
                }
            }

            public class Proxy2 implements Action2 {
                    private Action2 realObject2;
                 
                    public Proxy2(Action2 realObject2) {
                        this.realObject2 = realObject2;
                    }
                    public void doAnotherThing() {
                        System.out.println("proxy2 do");
                        realObject2.doAnotherThing();
                    }
                }


            3)为第三个方法创建代理类
            

public interface Action3 {
                public void doTwoAnotherThing();
            }
            
            public class RealObject3 implements Action3{
             
                public void doTwoAnotherThing() {
                    System.out.println("do two AnotherThing");
                }
            }

            public class Proxy3 implements Action3 {
                    private Action3 realObject3;
                 
                    public Proxy3(Action3 realObject3) {
                        this.realObject3 = realObject3;
                    }
                    public void doTwoAnotherThing() {
                        System.out.println("proxy3 do");
                        realObject3.doTwoAnotherThing();
                    }
                }

  调用:    

        Proxy proxy = new Proxy();
        proxy.doSomething();
        Proxy2 proxy2 = new Proxy2();
        proxy2.doAnotherThing();
        Proxy3 proxy3 = new Proxy3();
        proxy3.doTwoAnother();

        方案二:
          

 public interface Action {
                public void doSomething();
            }
            public interface Action2 {
                    public void doAnotherThing();
            }
            public interface Action3 {
                public void doTwoAnotherThing();
            }
            
            public class RealObject implements Action{
             
                public void doSomething() {
                    System.out.println("do something");
                }
            }
            public class RealObject2 implements Action2{
             
                public void doAnotherThing() {
                    System.out.println("do AnotherThing");
                }
            }
            
            public class RealObject3 implements Action3{
             
                public void doTwoAnotherThing() {
                    System.out.println("do two AnotherThing");
                }
            }

            public class Proxy implements Action,Action2,Action3{
                    private Action realObject;
                    private Action2 realobject2;
                    private Action3 realObject3;
                    private Object object;
                    public Proxy(Object Object) {
                        this.object = object;
                    }
                    if(Object instanceof RealObject){
                     Action realObject = (RealObject)object;
                     realObject.doSomething();
                    }else if(Object instanceof RealObject2)
                    {
                     Action2 realObject2 = (RealObject2)object;
                     realObject2.doAnotherThing();
                    }else{
                     Action3 realObject3 = (RealObject3)object;
                     realObject3.doTwoAnotherThing();
                    }
                }


缺点:为了扩展同样的功能,在方案一中,重复创建多个逻辑相同,仅仅RealObject引用不同的Proxy。

      方案二中,会导致proxy的膨胀,而且这种膨胀往往是无意义的。(此外,假如方法签名是相同的,更需要在调用的时候引入额外的判断逻辑)
                
java动态代理:运行时生成代理类(proxy的创建都是自动的并且是在运行期生成的)
             1)避免产生重复的代理类
             2)避免代理类的过度膨胀    (随着扩展,方法量不停的增加)

            public interface Action {
                public void doSomething();
            }
            public interface Action2 {
                    public void doAnotherThing();
            }
            public interface Action3 {
                public void doTwoAnotherThing();
            }
            
            public class RealObject implements Action{
             
                public void doSomething() {
                    System.out.println("do something");
                }
            }
            public class RealObject2 implements Action2{
             
                public void doAnotherThing() {
                    System.out.println("do AnotherThing");
                }
            }
            
            public class RealObject3 implements Action3{
             
                public void doTwoAnotherThing() {
                    System.out.println("do two AnotherThing");
                }
            }             
             
            public class DynamicProxyHandler implements InvocationHandler {
                private Object realObject;
             
                public DynamicProxyHandler(Object realObject) {
                    this.realObject = realObject;
                }
             
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    //代理扩展逻辑
                    System.out.println("proxy do");
                    return method.invoke(realObject, args);
                }
            }


            public static void main(String[] args) {
                    RealObject realObject = new RealObject();
                    Action proxy = (Action) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Action.class}, new DynamicProxyHandler(realObject));
                    proxy.doSomething();
                    
                    RealObject2 realObject2 = new RealObject2();
                    Action2 proxy2 = (Action) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Action2.class}, new DynamicProxyHandler(realObject2));
                    proxy2.doAnotherThing();

                    RealObject3 realObject3 = new RealObject3();
                    Action3 proxy3 = (Action) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Action3.class}, new DynamicProxyHandler(realObject3));
                    proxy3.doAnotherThing();
            }

借鉴:https://blog.csdn.net/WangQYoho/article/details/77584832

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值