静态代理实现

package proxy.staticproxy;

public interface Start {

    //面谈
    void confer();
    //签合同
    void singContract();
    //定票
    void bookTicket();
    //唱歌
    void sing();
    //收钱
    void collectMoney();
}
package proxy.staticproxy;

public class RealStar implements Start {

    @Override
    public void confer() {
        System.out.println("realStar confer");
        
    }

    @Override
    public void singContract() {
        System.out.println("realStar csingContractonfer");
        
    }

    @Override
    public void bookTicket() {
        System.out.println("realStar bookTicket");
        
    }

    @Override
    public void sing() {
        System.out.println("realStar sing");
        
    }

    @Override
    public void collectMoney() {
        System.out.println("realStar collectMoney");
        
    }

}
package proxy.staticproxy;

public class ProxyStar implements Start {
    private Start star;
    public ProxyStar(Start star) {
        this.star = star;
    }
    @Override
    public void confer() {
        System.out.println("ProxyStar confer");

    }
    

    @Override
    public void singContract() {
        System.out.println("ProxyStar singContract");

    }

    @Override
    public void bookTicket() {
        System.out.println("ProxyStar bookTicket");

    }

    @Override
    public void sing() {
        star.sing();
    }

    @Override
    public void collectMoney() {
        System.out.println("ProxyStar collectMoney");

    }

}
package proxy.staticproxy;

public class Client {
    public static void main(String[] args) {
        Start real = new RealStar();
        
        Start proxy = new ProxyStar(real);
        proxy.confer();
        proxy.singContract();
        proxy.bookTicket();
        proxy.sing();
        proxy.collectMoney();
    }
}



动态代理
package proxy.dynamitProxy;

public interface Start {

    //面谈
    void confer();
    //签合同
    void singContract();
    //定票
    void bookTicket();
    //唱歌
    void sing();
    //收钱
    void collectMoney();
}

package proxy.dynamitProxy;

public class RealStar implements Start {

    @Override
    public void confer() {
        System.out.println("realStar confer");
        
    }

    @Override
    public void singContract() {
        System.out.println("realStar csingContractonfer");
        
    }

    @Override
    public void bookTicket() {
        System.out.println("realStar bookTicket");
        
    }

    @Override
    public void sing() {
        System.out.println("realStar sing");
        
    }

    @Override
    public void collectMoney() {
        System.out.println("realStar collectMoney");
        
    }

}
package proxy.dynamitProxy;

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

public class StartHander implements InvocationHandler{

    Start realStart;
    
    public StartHander(Start realStart) {
        super();
        this.realStart = realStart;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = null;
        System.out.println("真正的方法执行前");
        System.out.println("面谈、签合同、订机票、预付款");
        if(method.getName().equals("sing")){
            invoke = method.invoke(realStart, args);//激活方法
        }
        return invoke;
    }

}
package proxy.dynamitProxy;

import java.lang.reflect.Proxy;

public class Client {
    public static void main(String[] args) {
        Start realStart = new RealStar();
        StartHander hander = new StartHander(realStart);
        Start proxy =(Start)Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Start.class}, hander);
        proxy.bookTicket();
        proxy.sing();
    }
}