|
From: roger h. <apo...@sn...> - 2003-11-21 01:50:01
|
Hi Colin
Your thoughts on implementation hiding have got me thinking...
But before I test anything could you clarify the sort of usage you
have in mind - specifically, would it make a difference whether the
implementation hiding factories were themselves 'typed' or 'generic' ie. do
you want / need the type of the factory to vary with the type that it
generates, or would a single generic factory be adequate ?
In other words, does one or other of the following make a difference ?
1. Typed interface factories:
interface MyFirstInterfaceFactory {
MyFirstInterface getInstance()
}
interface MySecondInterfaceFactory {
MySecondInterface getInstance()
}
2. Generic interface factory:
interface GenericFactory {
Object getInstance()
Class getClass()
}
where different instances of GenericFactory will return from getClass() the
relevant type, MyFirstInterface, MySecondInterface etc..
I'll try & test something tomorrow
Roger
"Colin Sampaleanu" <col...@ex...> wrote in message
news:3FB...@ex......
> I think I may be missing something, but I think it's desireable to be
> able to create in an easy fashion, 'closures' which encapsulate getting
> a bean from a bean factory.
>
> Say you have an interface
> interface MyInteface { ... whatever }
>
> and you have a factory Interface
> interface MyInterfaceFactory {
> MyInterface getInstance();
> }
>
> And you have a user of the factory, who you'd rather have no knowledge
> of Spring, which is why he's using the factory instead of calling
> getBean himself:
> class User {
> MyInterfaceFactory _myfac;
> public void setMyInterfaceFactory(MyInterfaceFactory myfac) { _myfac =
> myfac; }
> public void someMethod() {
> // need a new instance of MyInterface to work with
> MyInterface myint = _myfac.getInstance();
> ...
> }
> }
>
> Now I have a bean factory
> <beans>
> <bean id="myinterface" singleton="false"
> class="com.whatever.MyInterfaceImpl">
> </bean>
> <bean id="user" class="com.whatever.User">
> <property name="myInterfaceFactory"><ref bean="xxxxxxxx"/></property>
> </bean>
> </beans>
>
> now, as per the above, context.getBean("myinterface") is already a
> factory for objects implementing MyInterface. But I don't want the User
> object to know anything about contexts. And I'd rather not create an
> actual object that implements MyInterfaceFactory. It seems like a waste,
> since all I am doing here is trying to create a level of indirection,
> and I already have a factory inside the context itself, and I may want
> to use this approach in 30 different places, just to add a level of
> indirection in creating new objects.
>
> So what I think is needed is some variation of ProxyFactoryBean (but a
> separate class), which given a target bean (which is itself a factory),
> and a factory interface having a method with no args which returns a
> certain type, creates on the fly a new class implementing the factory
> interface, which will just use the target factory bean to actually
> supply the instance. So the bean def above would become:
> <beans>
> <bean id="myinterface" singleton="false"
> class="com.whatever.MyInterfaceImpl">
> </bean>
> <bean id="myinterface-factory"
class="org.springframework.whatever.XXXX">
> <property name="targetBean"><ref bean="xxxxxxxx"/></property>
> <property
> name="interface"><value>x.y.z.AFactoryInterface</value></property>
> </bean>
> <bean id="user" class="com.whatever.User">
> <property name="myInterfaceFactory"><ref
> bean="myinterface-factory"/></property>
> </bean>
> </beans>
>
> Am I missing an existing way to do this? Is this worth adding to spring
> as a convenience built-in, along the lines of TransactionProxyFactoryBean?
>
>
>
>
> -------------------------------------------------------
> This SF. Net email is sponsored by: GoToMyPC
> GoToMyPC is the fast, easy and secure way to access your computer from
> any Web browser or wireless device. Click here to Try it Free!
> https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22lp.tmpl
|