崇之他和她

导航

day036spring5AOP

AOP 与spring ioc控制反转机制 同等重要的核心

例子1

package com.zhq;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 18:54 2021/2/25
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public interface Man1s {
    public void select();
    public void delete();
    public void update();
    public void save();
}
package com.zhq;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 18:25 2021/2/25
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public class Man1 implements Man1s{
    //我有很多方法

    @Override
    public void select() {
        System.out.println("man1的查询方法");
    }

    @Override
    public void delete() {
        System.out.println("man1的删除方法");
    }

    @Override
    public void update() {
        System.out.println("man1的更新方法");
    }

    @Override
    public void save() {
        System.out.println("man1的保存方法");
    }
}
package com.zhq;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 18:56 2021/2/25
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public class Man2 implements Man1s {

    private Man1 man1;

    public void setMan1(Man1 man1) {
        this.man1 = man1;
    }

    @Override
    public void select() {
        printl(this+"");
        man1.select();

    }

    @Override
    public void delete() {
        printl(this+"");
        man1.delete();
    }

    @Override
    public void update() {
        printl(this+"");
        man1.update();
    }

    @Override
    public void save() {
        printl(this+"");
        man1.save();
    }

    private void printl(String methord){
        System.out.println("打印方法:执行了"+methord+"方法");
    }
}
class DoMan{
    public static void main(String[] args) {
        Man1 man1 = new Man1();
        man1.delete();//直接调用
        //需求不改动源代码,在man1类执行任何方法是输出打印日志
        //新增man2 类实现同一个接口  在实现类中增加打印日志  这样调用便可以不改动源代码,但是会使代码量翻倍
        Man2 man2 = new Man2();
        man2.setMan1(man1);
        man2.delete();
    }
}

例子2

package com.zhq;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 18:06 2021/2/25
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public interface Aopexe {
    //我是接口 ,真实实例和代理者都将实现共同方法
    public void getFish();//妹子向代理商买鱼
}
package com.zhq;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 18:09 2021/2/25
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public class Girl implements Aopexe{
    @Override
    public void getFish() {
        System.out.println("i am girl butiful i will buy a fish");
    }
}
package com.zhq;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 18:10 2021/2/25
 * @ Description:
 * @ Modified By:
 * @Version: $version$
 */
public class Shelled implements Aopexe {

    private Girl girl;

    public Shelled(Girl girl) {
        this.girl = girl;
    }

    @Override
    public void getFish() {
        makeFish();
        girl.getFish();
        haveMoney();
    }

    private void makeFish(){
        System.out.println("cooked the fish");
    }
    private void haveMoney(){
        System.out.println("the fish is shelled i have money");
    }
}
class Test{
    public static void main(String[] args) {
        //代理模式可以使真是实例专心只做自己的事情,代理做他自己的事并包含真实实例
        Shelled shelled = new Shelled(new Girl());
        shelled.getFish();
    }
}

posted on 2021-02-25 20:02  崇之他和她  阅读(68)  评论(0编辑  收藏  举报