java中 静态代理模式

编写代码

package com.xiang.lesson07;

public class StacticProxy {
    public static void main(String[] args) {
        Wedding wedding = new Wedding();
        wedding.Wedding(new You());
        wedding.HappyMarry();
    }
}

interface Marry {
    void HappyMarry();
}

//真实角色
class You implements Marry {
    @Override
    public void HappyMarry() {
        System.out.println("marry...........");
    }
}

//代理角色
class Wedding implements Marry {
    private Marry target;

    public Wedding(){

    }


    public void Wedding(Marry target) {
        this.target = target;
    }

    @Override
    public void HappyMarry() {
        before();
        this.target.HappyMarry();
        after();
    }

    private void before() {
        System.out.println("before...........");
    }

    private void after() {
        System.out.println("after..............");
    }
}

运行结果

posted @ 2021-09-07 20:54  阿向向  阅读(19)  评论(0编辑  收藏  举报