The enhanced MoneyFactory.

Power Java
Dynamic Proxy Classes:
Toward Metalevel Programming in Java

Mathias Richter and Takashi Suezawa
Listing 8. The enhanced MoneyFactory.


public class MoneyFactory
{

	private ProxyFactory fProxyFactory;
	private boolean fLog;

	public MoneyFactory()
	{
		fProxyFactory = new ProxyFactory();
		fLog = false;
	}
	protected IMoney internalNewMoney (String isoCurrency, double amount )
	{
		return new Money( isoCurrency, amount );
	}
	public IMoney newMoney(String isoCurrency, double amount )
	{
		return ( IMoney )fProxyFactory.create( internalNewMoney(
                             isoCurrency, amount ), null, IMoney.class );
	}
	public IMoney newLoggedMoney (String isoCurrency, double amount )
	{
		return ( IMoney )fProxyFactory.create (internalNewMoney( isoCurrency, amount ),
                                    new MoneyLogger(), IMoney.class );
	}
	public void log( boolean on )
	{
		if ( fLog == on )
			return;
		fLog = on;
		if ( fLog )
			fProxyFactory.addAspect( new MoneyLogger() );
		else
			fProxyFactory.removeAspect( MoneyLogger.class );
	}
}