MoneyFactory.

Power Java
Dynamic Proxy Classes:
Toward Metalevel Programming in Java

Mathias Richter and Takashi Suezawa
Listing 4. MoneyFactory.


public class MoneyFactory
{
   public static IMoney newMoney(String currency, double amount)
   {
	    IMoney result = null;
	    try
       {
         MoneyInvocationHandler handler = null;
         Money primary = new Money( currency, amount );
         handler = new MoneyInvocationHandler (primary);
         result = (IMoney) Proxy.newProxyInstance(primary.getClass().getClassLoader(),
                 new Class[] { IMoney.class }, handler);
	    } catch(Exception e)
          {
          System.out.println( e.getMessage() );
        } finally
        {
          return result;
       }
   }
}