面试题复习-常用设计模式-动态代理
被代理类实现的接口:
package com.ajax.pattern.proxy; /** * 票务 */ public interface Ticketmaster { void sell(); }
被代理类:
package com.ajax.pattern.proxy; /** * 大麦网 */ public class DaMai implements Ticketmaster { @Override public void sell() { System.out.println("大麦网出票"); } }
代理:
package com.ajax.pattern.proxy; /** * 大麦网 */ public class DaMai implements Ticketmaster { @Override public void sell() { System.out.println("大麦网出票"); } }
运行:
package com.ajax.pattern.proxy; import java.lang.reflect.Proxy; public class Run { public static void main(String[] args) { Ticketmaster p = (Ticketmaster) Proxy .newProxyInstance(DaMai.class.getClassLoader(), DaMai.class.getInterfaces(), new Cattle(new DaMai())); p.sell(); } }