= Framework =
public FactoryProvidesInvalidClassException
extends DependencyException // RuntimeException...
{
protected Class m_provided; public FactoryProvidesInvalidClassException(
Class required, Class provided )
{
super( required );
m_provided = provided;
}
public String getMessage()
{
if( getRequired() != null && m_provided != null )
return "Required a " + getRequired().getName() + " instance " +
"to be returned from the Factory, but the factory " +
"provides instances of " + m_provided.getName();
else
return super.getMessage();
}
}
public interface Factory
{
Object get();
void release( Object o );
void required( Class class )
throws CannotProvidedRequiredClassException;
}
public abstract class AbstractFactory implements Factory
{
protected Class m_providedClass = null; protected AbstractFactory() {}
protected AbstractFactory( Class providedClass )
{
setProvidedClass( providedClass );
} protected void setProvidedClass( Class providedClass )
{
Assert.assertNotNull( providedClass );
m_providedClass = providedClass;
} public void required( Class class )
{
if( m_providedClass != null && !m_providedClass.equals( class ) )
throw new FactoryProvidesInvalidClassException(
class );
}
} public class PoolBackedFactory extends AbstractFactory
{
protected Pool m_pool; public PoolBasedFactory( Pool pool )
{
this( pool, null );
}
public PoolBasedFactory( Pool pool, Class providedClass )
{
super( providedClass );
setPool( pool );
} protected void setPool( Pool pool )
{
Assert.assertNotNull( pool );
m_pool = pool;
} public void release( Object obj )
{
m_pool.release( obj );
}
public Object get()
{
return m_pool.get();
}
}= client side =
== container *and* client validation (optional, but recommended) ==
public interface Client { doStuff(); }
public class MyClient implements Client
{
protected Factory m_tFactory; /** @@.f Dependency( SAXTransformer.class ) */
protected MyClient() {}
public MyClient( Factory tFactory )
{
setFactory( tFactory );
} protected void setFactory( Factory tFactory )
{
f.required( SaxTransformer.class );
m_tFactory = tFactory;
} public void doStuff()
{
SAXTransformer t = (SAXTransformer)m_tFactory.get();
doStuffWithT( t );
m_tFactory.release( t );
} protected void doStuffWithT( SAXTransformer t ) { /* ... */ }
}== alternative: no validation (not recommended ) ==
public MyClient( Factory f ) // fragment
{
m_factory = f;
}== alternative: client-side validation (optional, but recommended) ==
public MyClient( Factory f ) // fragment
{
f.required( SAXTransformer.class );
m_factory = f;
}== alternative: container-side validation (optional, but recommended) ==
/** @@.f Dependency( SAXTransformer.class ) */
public MyClient( Factory f ) // fragment
{
m_factory = f;
}== adding avalon-framework semantics ==
public class MyAvalonClient extends MyClient
{
public MyAvalonClient() {} /** @@Dependency( SAXTransformer.FACTORY_ROLE ) */
public void service( ServiceManager sm )
throws ServiceException
{
try
{
Factory f = (Factory)sm.lookup( SAXTransformer.FACTORY_ROLE );
}
catch( DependencyException de )
{
throw new ServiceException( SAXTransformer.FACTORY_ROLE,
"bad factory", de );
}
}
}I (still) think ServiceException should've been a RuntimeException :D.
== adding JavaBean/XWork/Spring semantics ==
public class MyBeanClient extends MyClient implements FactoryAware
{
/** @@Dependency( SAXTransformer.class ) */
public void setFactory( Factory f ){ super.setFactory( f ); }
}This seems about as clean as we can make it. Not much of an RT anymore now, eh? :D
-- cheers,
- Leo Simons
----------------------------------------------------------------------- Weblog -- http://leosimons.com/ IoC Component Glue -- http://jicarilla.org/ Articles & Opinions -- http://articles.leosimons.com/ ----------------------------------------------------------------------- "We started off trying to set up a small anarchist community, but people wouldn't obey the rules." -- Alan Bennett
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
