Aop(一)面向切面编程理解和动态代理

2018-07-23  本文已影响0人  whoisje

此文章为本人原创,转载请说明出处。
https://www.jianshu.com/p/517416271be5
作者:敲破电脑问到底

面向切面编程的理解

package com.jack.aop;

/**
 * @author wangwenj
 */
public class Programmer implements People {
    public String name;

    /**
     * init name
     *
     * @param name name
     */
    public Programmer(String name) {
        this.name = name;
    }

    /**
     * eat something
     */
    @Override
    public void eatBreakfast() {
        Washing.brushTeeth(name);
        System.out.println(name+" is eating breakfast");
        
    }

    /**
     * eat at night
     */
    @Override
    public void eatDinner() {
        Washing.brushTeeth(name);
        System.out.println(name+" is eating dinner");
    }
}

Washing是他刷牙的方法

package com.jack.aop;

/**
 * @author wangwenj 
 */
public class Washing{
    /**
     * 刷牙
     * @param name 名字
     */
    public static void brushTeeth(String name) {
        System.out.println(name+" is brushing teeth now.");
    }
}

所以程序员的每天是这样的

public class Daily {
    public static void main(String[] args){
        People programmer = new Programmer("old wang");
        programmer.eatBreakfast();
        programmer.eatDinner();
    }
}

结果是下面这样的

old wang is brushing teeth now.
old wang is eating breakfast
old wang is brushing teeth now.
old wang is eating dinner
package com.jack.aop;

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

public class WashingHandler implements InvocationHandler {
    /**
     * 目标对象
     */
    public Object target;
    private String name;

    public WashingHandler(Object target, String name) {
        this.target = target;
        this.name = name;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        /**
         * 把brushTeeth统一植入到method的前面
         */
        Washing.brushTeeth(name);
        /**
         * 调用method
         */
        method.invoke(target, args);
        return null;
    }
}

这样他就不用重复的写刷牙的代码

package com.jack.aop;

/**
 * @author wangwenj
 */
public class Programmer implements People {
    public String name;

    /**
     * init name
     *
     * @param name name
     */
    public Programmer(String name) {
        this.name = name;
    }

    /**
     * eat something
     */
    @Override
    public void eatBreakfast() {
        //Washing.brushTeeth(name);
        System.out.println(name+" is eating breakfast");

    }

    /**
     * eat at night
     */
    @Override
    public void eatDinner() {
        //Washing.brushTeeth(name);
        System.out.println(name+" is eating dinner");
    }
}

然后他的日常变成了这样

package com.jack.aop;

import java.lang.reflect.Proxy;

/**
 * @author wangwenj
 * daily eat
 */
public class Daily {
    public static void main(String[] args) {
        People people = new Programmer("old wang");
        WashingHandler handler = new WashingHandler(people, "old wang");
        People programmer = (People) Proxy.newProxyInstance(people.getClass().getClassLoader(), people.getClass().getInterfaces(), handler);
        programmer.eatBreakfast();
        programmer.eatDinner();
    }
}

结果和原来的一样

old wang is brushing teeth now.
old wang is eating breakfast
old wang is brushing teeth now.
old wang is eating dinner
上一篇下一篇

猜你喜欢

热点阅读