【Java笔记(25)】工厂设计模式与代理设计模式和Annotation整合

一、核心代码

  1. 动态代理类
/**
 * 动态代理类
 * @Author Xlu
 * @Date 2020-06-16 23:53
 * @ClassName MessageProxy
 * @Version 11
 **/
class MessageProxy implements InvocationHandler {
    private Object target;
    public Object bind(Object target) {
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader() , target.getClass().getInterfaces() , this);
    }
    public void close() {
        System.out.println("【代理类】关闭连接");
    }
    public boolean connect() {
        System.out.println("【代理类】连接成功!");
        return true;
    }
    
    @Override
    public Object invoke(Object proxy , Method method , Object[] args) throws Throwable {

        Object returnObj = null;
        try {
            if (connect()) {

                /**
                 * 这里要使用 this.target 不能使用参数里的 proxy
                 * 【X】returnObj = method.invoke(proxy , args);
                 */
                returnObj = method.invoke(this.target , args);
            }
        } finally {
            close();
        }
        return returnObj;
    }
}
  1. 动态工厂类

/**
 * 动态工厂类
 * @Author Xlu
 * @Date 2020-06-17 23:20
 * @ClassName Factory
 * @Version 11
 **/
class Factory {
    private Factory() {
    }
    public static <T> T getInstance(Class<T> clazz) {
        try {
            //绑定一个代理实例
            return (T) new MessageProxy().bind(clazz.getDeclaredConstructor().newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

  1. 整合代码
//自定义的用户消息Annotation
@Retention(RetentionPolicy.RUNTIME)
@interface UserMessage {
    public Class<?> clazz();
}
//更改注解就能起来更改结果
@UserMessage(clazz = NetMessage.class)
//@UserMessage(clazz = Message.class)
class MessageService{
    private  IMessage message;

    public  MessageService() throws NoSuchMethodException {
        //获取本类的Annotation
        UserMessage anno = MessageService.class.getAnnotation(UserMessage.class);
        //将本类的Annotation中的值获取
        this.message=(IMessage) Factory.getInstance(anno.clazz());
    }
    public void spend(String msg) {
        this.message.spend(msg);
    }
}

二、完整代码

package reflect.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 *
 * 代理设计模式与工厂设计模式整合Annotation实现
 * @ClassName: FactoryProxyAnnotation
 * @Author: Xlu
 * @Date: 2020-06-16 23:48
 * @Version 11
 **/
public class FactoryProxyAnnotation {
    public static void main(String[] args) throws NoSuchMethodException {
        MessageService messageService = new MessageService();
        messageService.spend("xlu");
    }
}




/**
 * 消息类接口
 * @Author Xlu
 * @Date 2020-06-17 23:19
 * @ClassName IMessage
 * @Version 11
 **/
interface IMessage {
    public void spend(String msg);
}

/**
 * 自定义的用户消息Annotation
 * @author Xlu
 */
@Retention(RetentionPolicy.RUNTIME)
@interface UserMessage {
    public Class<?> clazz();
}

@UserMessage(clazz = NetMessage.class)
//@UserMessage(clazz = Message.class)
class MessageService{
    private  IMessage message;

    public  MessageService() throws NoSuchMethodException {
        //获取本类的Annotation
        UserMessage anno = MessageService.class.getAnnotation(UserMessage.class);
        //将本类的Annotation中的值获取
        this.message=(IMessage) Factory.getInstance(anno.clazz());
    }

    public void spend(String msg) {
        this.message.spend(msg);
    }
}

/**
 * 动态工厂类
 * @Author Xlu
 * @Date 2020-06-17 23:20
 * @ClassName Factory
 * @Version 11
 **/
class Factory {
    private Factory() {
    }

    /**
     * @param clazz     接口class
     * @return T 返回一个代理实例
     * @Author Xlu
     * @Date 0:10 2020-06-17
     */
    public static <T> T getInstance(Class<T> clazz) {
        try {
            //绑定一个代理实例
            return (T) new MessageProxy().bind(clazz.getDeclaredConstructor().newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

/**
 * 真实业务类
 * @Author Xlu
 * @Date 2020-06-16 23:54
 * @ClassName Message
 * @Version 11
 **/
class Message implements IMessage {
    /**
     * 一定要有构造方法
     * @Author Xlu
     * @Date 0:10 2020-06-17
     */
    public Message() {
    }

    @Override
    public void spend(String msg) {
        System.out.println("【普通消息】"+msg);
    }
}

class NetMessage implements IMessage{
    public NetMessage() {
    }
    @Override
    public void spend(String msg) {
        System.out.println("【网络消息】"+msg);
    }
}
/**
 * 代理类,用于处理其他业务
 *
 * @Author Xlu
 * @Date 2020-06-16 23:53
 * @ClassName MessageProxy
 * @Version 11
 **/
class MessageProxy implements InvocationHandler {
    /**
     * 真实业务对象
     */
    private Object target;

    public Object bind(Object target) {
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader() , target.getClass().getInterfaces() , this);
    }

    /**
     * 关闭所有连接
     *
     * @return void
     * @Author Xlu
     * @Date 23:53 2020-06-16
     */
    public void close() {
        System.out.println("【代理类】关闭连接");
    }

    /**
     * 连接方法
     *
     * @return boolean
     * @Author Xlu
     * @Date 23:51 2020-06-16
     */
    public boolean connect() {
        System.out.println("【代理类】连接成功!");
        return true;
    }

    /**
     * @param proxy  代理对象
     * @param method 代理方法
     * @param args   方法参数
     * @return java.lang.Object 返回方法结果
     * @Author Xlu
     * @Date 0:13 2020-06-17
     */
    @Override
    public Object invoke(Object proxy , Method method , Object[] args) throws Throwable {

        Object returnObj = null;
        try {
            if (connect()) {

                /**
                 * 这里要使用 this.target 不能使用参数里的 proxy
                 * 【X】returnObj = method.invoke(proxy , args);
                 */
                returnObj = method.invoke(this.target , args);
            }
        } finally {
            close();
        }
        return returnObj;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值