设计模式——代理模式

9 篇文章 0 订阅
8 篇文章 0 订阅

基础接口

UserService 接口

public interface UserService {
    void deleteUser(String userId);
}

UserServiceImpl

public class UserServiceImpl implements UserService {
    @Override
    public void deleteUser(String userId) {
        System.out.println("delete user " + userId);
    }
}

静态代理

UserServiceProxy

public class UserServiceProxy implements UserService {

    private UserService userService;

    public UserServiceProxy(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void deleteUser(String userId) {
        System.out.println("UserServiceProxy before ...........");
        userService.deleteUser(userId);
        System.out.println("UserServiceProxy after ...........");
    }
}

UserServiceProxyTest

public class UserServiceProxyTest {
    public static void main(String[] args) {
        UserService proxy = new UserServiceProxy(new UserServiceImpl());
        proxy.deleteUser("1");
    }
}

动态代理

JDK动态代理

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


public class UserServiceInvocationHandler implements InvocationHandler {

    private Object object;

    public UserServiceInvocationHandler(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("UserServiceInvocationHandler before ...........");
        Object result = method.invoke(object, args);
        System.out.println("UserServiceInvocationHandler before ...........");
        return result;
    }
}
JdkProxyTest
import java.lang.reflect.Proxy;
public class JdkProxyTest {

    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        UserServiceInvocationHandler userServiceInvocationHandler = new UserServiceInvocationHandler(userService);
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(UserService.class.getClassLoader(),
                new Class[]{UserService.class}, userServiceInvocationHandler);
        userServiceProxy.deleteUser("1");
    }
}

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


public class ProxyFactory {
    public static Object getProxy(Object object) {
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),
                object.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("ProxyFactory before ..............");
                        Object result = method.invoke(object, args);
                        System.out.println("ProxyFactory after ..............");
                        return result;
                    }
                });
    }
}
ProxyFactoryTest
public class ProxyFactoryTest {
    public static void main(String[] args) {
        UserService proxy = (UserService) ProxyFactory.getProxy(new UserServiceImpl());
        proxy.deleteUser("1");
    }
}

CGLIB动态代理

引入cglib依赖
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>2.1_2</version>
    </dependency>
UserServiceMethodInterceptor
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
public class UserServiceMethodInterceptor implements MethodInterceptor {

    private Object object;

    public UserServiceMethodInterceptor(Object object) {
        this.object = object;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("UserServiceMethodInterceptor before ...........");
        Object result = method.invoke(object, objects);
        System.out.println("UserServiceMethodInterceptor after ...........");
        return result;
    }
}
CglibProxyTest
import net.sf.cglib.proxy.Enhancer;
public class CglibProxyTest {
    public static void main(String[] args) {
        UserServiceImpl userServiceImpl = new UserServiceImpl();
        UserService userService = (UserService) Enhancer.create(userServiceImpl.getClass(), new UserServiceMethodInterceptor(userServiceImpl));
        userService.deleteUser("1");
    }
}

ProxyFactory
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class ProxyFactory {
    public static Object getProxy(Object object) {
        return Enhancer.create(object.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("ProxyFactory before ..............");
                Object result = method.invoke(object, objects);
                System.out.println("ProxyFactory after ..............");
                return result;
            }
        });
    }
}
ProxyFactoryTest
public class ProxyFactoryTest {
    public static void main(String[] args) {
        UserService userService = (UserService) ProxyFactory.getProxy(new UserServiceImpl());
        userService.deleteUser("1");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值