设计模式的应用

/**
 *  单例 + 工厂 + 构造者 + 动态代理
 */
public class HandelrFactory {


    /**
     * 接口或者接口的实现类
     */
    private final Class<? extends IHandler> clazz;

    private final Object[] params;

    /**
     * 调用此方法 会调用静态内部类 调用静态属性方法
     * @return
     */
    private static Builder newInstance(){
        return Instance.instance;
    }

    /**
     * 通过builder去调用方法 new对象
     * @return
     */
    public static Builder builder(){
        return newInstance();
    }

    private HandelrFactory(Class<? extends IHandler> clazz, Object ... params){
        this.params = params;
        this.clazz = clazz;
    }

    /**
     * 静态内部类
     */
    private static class Instance {
        public static final Builder instance = new Builder();
    }

    /**
     * 运行方法
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public void bind() throws IllegalAccessException, InstantiationException {
        //实例化字节码
        IHandler myHandler = this.clazz.newInstance();
        //动态代理
        InvocationHandler invocationHandler = new MyInvocationHandler(myHandler);
        //代理对象  1. 用来指明生成代理对象使用哪个类装载器 2.Class<?>[] interfaces用来指明生成哪个对象的代理对象 3. 用来指明产生的这个代理对象要做什么事情
        IHandler result = (IHandler) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{IHandler.class}, invocationHandler);
        //代理对象执行方法
        result.executor(this.params);
    }

    static class Builder {

        private Class<? extends IHandler> clazz;

        private Object[] params;

        /**
         * set方法1
         * @param clazz
         * @return
         */
        public Builder setClazz(Class<? extends IHandler> clazz){
            this.clazz = clazz;
            return this;
        }

        /**
         * set方法2
         * @param params
         * @return
         */
        public Builder setParams(Object ... params){
            this.params = params;
            return this;
        }

        /**
         * 根据传入参数构建工厂
         * @return
         */
        public HandelrFactory build(){
            return new HandelrFactory(this.clazz, this.params);
        }

    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        HandelrFactory
                .builder()
                .setClazz(MyHandlerImpl.class)
                .setParams("12345")
                .build()
                .bind();

        System.out.println();

        HandelrFactory
                .builder()
                .setClazz(MyHandlerImpl.class)
                .setParams("fdsfds")
                .build()
                .bind();
    }
}

 

接口:

public interface IHandler {

    Object executor(Object... param);
}

 

接口实现类:

public class MyHandlerImpl implements IHandler {

    @Override
    public Object executor(Object... param) {
        for(Object p: param){
            if (p instanceof String){
                System.out.println("我是接口实现类,我在执行方法");
               String paramValue = (String)p;
                System.out.println("方法的参数是"+paramValue);
            }
        }
        return null;
    }
}

动态代理类:

public class MyInvocationHandler implements InvocationHandler {

    //要增强的对象
    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
        System.out.println("代理对象进行方法增强");
        //某个方法 增强的目标 参数
        method.invoke(target, args);
        return null;
    }
}

 

posted @ 2019-07-12 22:37  toov5  阅读(218)  评论(0编辑  收藏  举报