User DocumentationOne minute descriptionTwo minute tutorial Five minute introduction Advanced Topics FAQ Container Components Terminology Mock Objects Inversion of Control Types PatternsInversion of ControlDependency Injection Constructor Injection Setter Injection Interface-Impl Separation Lifecycle Antipatterns Developer DocumentationHow To ContributeRelative Volunteering Release Process Project InformationSloganMailing lists Source Repositories Open Issues Blog entries Statistics Team Sister Projects TShirts MiscellaneousDifferentiatorsNirvana Full Sitemap |
Implementation hiding and Hot swappingAuthors: Aslak Hellesoy BasicsCircular 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 aImplementationHidingComponentAdapterFactory. ExampleConsider two classes, Wife and Husband:public class Wife implements Woman { public final Man man; public Wife(Man man) { this.man = man; } public Man getMan() { return man; } } public class Husband implements Man { public final Woman woman; public Husband(Woman woman) { this.woman = woman; } public int getEndurance() { return 10; } } public interface Woman { Man getMan(); } public interface Man { int getEndurance(); } We can register them both in PicoContainer, and thanks to ImplementationHidingComponentAdapter's lazy materialisation, we can have mutual dependencies: snippet: http://cvs.picocontainer.codehaus.org/checkout/java/picocontainer/src/test/org/picocontainer/doc/hotswapping/HotSwappingDemoTestCase.java?root=picocontainer
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). snippet: http://cvs.picocontainer.codehaus.org/checkout/java/picocontainer/src/test/org/picocontainer/doc/hotswapping/HotSwappingDemoTestCase.java?root=picocontainer
public class Superman implements Man { public int getEndurance() { return 1000; } } 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? |