- 可以提供隔离作用个,让客户无法接触到真正的源对象,也可以提供更大的扩展性。在不修改源对象的前提下扩展功能。
//interface
public interface Post {
public String byTicket();
}
//impl
public class Ticket implements Post {
@Override
public String byTicket(){
String ticket = UUID.randomUUID().toString().replaceAll("-", "");
System.out.println("买到了票:" + ticket);
return ticket;
}
}
//proxy
public class BootStrap {
public static void main(String[] args) {
Ticket ticket = new Ticket();
Post o = (Post) Proxy.newProxyInstance(Ticket.class.getClassLoader(), Ticket.class.getInterfaces(), (object, method, parm) -> {
System.out.println("收手续费50");
Object result = method.invoke(ticket, parm);
return result;
});
String s = o.byTicket();
}
}