1-06-21 IBM / 王忠平 何平
接着,我们将通过购票者类 TicketBuyer 来模拟购票以演示扩展动态代理机 制。
清单 20. TicketBuyer
public class TicketBuyer
{
public static void main(String[] args)
{
// 创建真正的TickerSeller对象,作为stub实体
final TicketSeller stub = new Expo2010TicketSeller ();
// 创建扩展调用处理器对象
InvocationHandler handler = new InvocationHandlerEx()
{
public Object getStub(Class stubClass)
{
// 仅对可接受的Class类型返回stub实体
if( stubClass.isAssignableFrom(stub.getClass()) )
{
return stub;
}
return null;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
Object o;
try
{
System.out.println(" >>> Enter method: "
+ method.getName() );
o = method.invoke(stub, args);
}
catch(InvocationTargetException e)
{
throw e.getCause();
}
finally
{
System.out.println(" <<< Exit method: "
+ method.getName() );
}
return o;
}
};
// 通过ProxyEx构造动态代理
TicketSeller seller = (TicketSeller) ProxyEx.newProxyInstance(
TicketBuyer.class.getClassLoader(),
new Class[] {TicketSeller.class},
handler);
// 显示代理类的类型
System.out.println("Ticket Seller Class: " + seller.getClass() + "\n");
// 直接访问theme变量,验证代理类变量在对象构造时同步的有效 性
System.out.println("Ticket Theme: " + seller.theme + "\n");
// 函数访问price信息
System.out.println("Query Ticket Price...");
System.out.println("Ticket Price: " + seller.getTicketPrice() + " RMB\n");
// 模拟票务交易
buyTicket(seller, 1, 200);
buyTicket(seller, 1, 160);
buyTicket(seller, 250, 30000);
// 直接更新theme变量
System.out.println("Updating Ticket Theme...\n");
seller.theme = "World Expo 2010 in Shanghai";
// 函数访问theme信息,验证扩展动态代理机制对变量同步的有效 性
System.out.println("Query Updated Ticket Theme...");
System.out.println("Updated Ticket Theme: " + seller.getTicketTheme() + "\n");
}
// 购票函数
protected static void buyTicket(TicketSeller seller, int ticketNumber, int
|