静态代理 动态代理 函数式平替

2023-05-16  本文已影响0人  zxbyh

设计模式里面的代理模式,这个模式的本质是需要解决代码 可变的的问题. 因为java8之前 只能是数据可变,不能代码可变. 导致弄出的设计模式. 在java8以后可以用lambda 代替了.

一 静态代理 ,有多少代理就要写多少个代理类 .


import lombok.RequiredArgsConstructor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//动态代理demo
public class StaticProxyDemo {
    //这个接口
    interface IGirl {
        void watchMovie();
    }

    //具体的一个将要被代理的对象
    static class Rose implements IGirl{
        public void watchMovie(){
            System.out.println("这个电影不好看");
        }
    }

    //代理对象
    @RequiredArgsConstructor
    static class GirlProxy implements IGirl{
        private final IGirl girl;

        private void _doSomeThingBefore(){
            System.out.println("审查这个家伙有不有钱?,人品如何?...审查通过");
        }

        private void _doSomeThingEnd(){
            System.out.println("这家伙干了什么? 是否规矩?...还比较可以");
        }

        public void watchMovie(){
            _doSomeThingBefore();
            this.girl.watchMovie();
            _doSomeThingEnd();
        }
    }

    public static void main(String[] args) {
        (new GirlProxy(new Rose())).watchMovie();
    }

}

二 动态代理


import lombok.RequiredArgsConstructor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//动态代理demo
public class DynamicProxyDemo {
    //这个接口
    interface IGirl {
        void watchMovie();
    }

    //具体的一个将要被代理的对象
    static class Rose implements IGirl{
        public void watchMovie(){
            System.out.println("这个电影不好看");
        }
    }

    interface ILady {
        void shopping();
    }

    //具体的一个将要被代理的对象
    static class Mary implements ILady{
        public void shopping(){
            System.out.println("购物愉快!");
        }
    }

    //代理对象
    @RequiredArgsConstructor
    static class DynamicProxy implements InvocationHandler{
        private final Object obj;

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            _doSomeThingBefore();
            Object rlt = method.invoke(this.obj,args);
            _doSomeThingEnd();
            return rlt;
        }

        private void _doSomeThingBefore(){
            System.out.println("doSomeThingBefore: 一般记录日志或者开事务等");
        }

        private void _doSomeThingEnd(){
            System.out.println("doSomeThingEnd: 一般记录日志或者事务提交等");
        }

        public Object getProxyInstance(){
            return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),this);
        }

    }

    public static void main(String[] args) {
        ((IGirl)(new DynamicProxy(new Rose()).getProxyInstance())).watchMovie();
        ((ILady)(new DynamicProxy(new Mary()).getProxyInstance())).shopping();
    }

}

三 用函数式编程lambda代替


import lombok.RequiredArgsConstructor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.function.Supplier;

//动态代理demo
public class FunctionAsProxyDemo {

    static class Rose {
        public String watchMovie(){
            System.out.println("这个电影不好看");
            return "不开心!";
        }
    }

    static class Mary {
        public String shopping(){
            System.out.println("购物愉快!");
            return "非常开心";
        }
    }

    //代理对象
    static class FuncAsProxy {

        private void _doSomeThingBefore(){
            System.out.println("doSomeThingBefore: 一般记录日志或者开事务等");
        }

        private void _doSomeThingEnd(){
            System.out.println("doSomeThingEnd: 一般记录日志或者事务提交等");
        }

        public Object doProxy(Supplier supplier){
            _doSomeThingBefore();
            Object rlt = supplier.get();
            _doSomeThingEnd();
            return rlt;
        }

    }

    public static void main(String[] args) {
        System.out.println(
            (new FuncAsProxy()).doProxy(()->new Rose().watchMovie())
        );

        System.out.println(
            (new FuncAsProxy()).doProxy(()->new Mary().shopping())
        );
    }

}


上一篇 下一篇

猜你喜欢

热点阅读