Authors: Aslak Hellesøy
WARNING! CODE IS PARTLY OUT OF DATE. NEEDS WORK.
![]()
Basics
Circular dependencies and transparent hot swapping is supported by ImplementationHidingComponentAdapter. In order to achieve this, your components must honour Interface Implementation Separation. It is then done simply by instantiating the container with a CachingComponentAdapterFactory around a ImplementationHidingComponentAdapterFactory.
Note: The ImplementationHidingComponentAdapter was removed from PicoContainer and is now located in the component NanoProxytoys.
Example
Consider two classes, Wife and Husband:
An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/Wife.java. The system administrator has been notified. An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/Husband.java. The system administrator has been notified. An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/Woman.java. The system administrator has been notified. An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/Man.java. The system administrator has been notified.We can register them both in PicoContainer, and thanks to ImplementationHidingComponentAdapter's lazy materialisation, we can have mutual dependencies:
An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/HotSwappingDemoTestCase.java. The system administrator has been notified.Note that we have to cast to the interface they implement, since what the container gives us back is actually dynamic proxies that forward to the real subjects (that are hidden from you). Here is a UML object diagram that describes the relationships between the objects in the system.

When a component is requested via getComponentInstance(), only a dynamic proxy for the real subject is created. Nothing more. This means that after line 7 is executed, the only object that exists is the proxy for Woman. The hidden delegates aren't instantiated until a method is called on the proxy. (The delegates are lazily instantiated).
After line 8 is executed, all 4 objects will exist. The call to getMan() will cause the real Wife subject to be instantiated. This will in turn instantiate the Man proxy to satisfy Wife's constructor. Finally, the call to getEndurance() will instantiate the Husband subject.
In addition to supporting mutual dependencies, ImplementationHidingComponentAdapterFactory also lets you hotswap the delegates. This can be done by casting the component instance to Swappable and pass in the new subject via the hotswap() method. (All proxy objects created by ImplementationHidingComponentAdapter implement Swappable).
An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/HotSwappingDemoTestCase.java. The system administrator has been notified. An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/hotswapping/Superman.java. The system administrator has been notified.This will swap out the Man proxy's current delegate with a new one. Completely transparently to the Wife object. When the wife now calls a methods on her Man, it will be delegated to a new instance. Scary?

Comments (1)
Jul 24, 2004
Jörg Schaible says:
TODO: Links to the ImplementationHidingCA above are wrong. they should point to ...TODO: Links to the ImplementationHidingCA above are wrong. they should point to nano-proxytoys.