Lifecycle Managers and Strategies

Lifecycle

PicoContainer handle the lifecycle aspects of components using two complementary concepts: Lifecycle Strategy and Lifecycle Manager.
The lifecycle strategy acts on a component instance and applies the lifecycle methods, while the lifecycle manager actos on container
instances and uses the container to get the component instance (resolving any dependencies if may require) and the delegates to the
injected lifecycle strategy to task of applying the lifecycle methods.

Lifecycle Strategies

The lifecycle strategy is represented by the interface LifecycleStrategy which, when implemented, handles the lifecycle aspects of components that a particular PicoContainer instance is managing. There are three methods that require implementation:

void start(Object component);

void stop(Object component);

void dispose(Object component);

PicoContainer will call start, stop, dispose as appropriate to start, stop or dispose when it may be appropriate for a component.

DefaultLifecycleStrategy

This class provides lifecycle functionality for components implementing the Startable and Disposable interfaces that ship with PicoContainer. The lifecycle methods are only invoked if the component implements Startable and Disposable.

public class Foo implements Startable {
  public void start() {
    // something
  }    
  public void stop() {
    // something
  }
}
public class Bar implements Disposable {
  public void dispose() {
  }
}

ReflectionLifecycleStrategy

This class provides lifecycle functionality for components that expose the start/stop/dispose methods but do not
implement the Startable and Disposable interfaces.

public class Foo {
  public void start() {
    // something
  }
  public void stop() {
    // something
  }
}
public class Bar {
  public void dispose() {
  }
}

ReflectionLifecycleStrategy is part of the Picocontainer Gems package.

Lifecycle Managers

LifecycleManager is an interface implemented by various ComponentAdapterFactories, ComponentAdapters, and PicoContainers.
Similarly to LifecycleStrategy, it has three methods but the argument passed in is the container rather than the component instance.

void start(PicoContainer container);

void stop(PicoContainer container);

void dispose(PicoContainer container);

A ComponentAdapter may or may not implement the LifecycleManager interface. If it does implement it, it will typically have
an injected LifecycleStrategy to delegate invocation of start/stop/dispose events.

Labels

 
(None)