Transfer from: http://www.picocontainer.org/two minute Tutorial
Two minute tutorial two minutes tutorial
Authors: Jon Tirsen
This Very Short Tutorial SHOULD GET You Up to Speed With Picocontainer In 2 Minutes. It Do It, Read The Five Minute Introduction for That. This is a very short thing that you can quickly master the microlator. Minute tutorial. If it still can't be you sure why you want to use a micro-capacity, you can continue to read 5 minutes.
Download and INSTALL
Download The Jar File and Include It in your classpath.
Two simple components of Write Two Simple Components
Public class boy {
Public void kiss (Object kisser) {
System.out.println ("I Was Kissed By" Kisser;
}
}
Public class girl {
Boy boy;
Public Girl (Boy Boy) {
THIS.BOY = BOY;
}
Public void kisssome () {
Boy.kiss (this);
}
}
Assemble Components Assembly Components
MutablePicocontainer Pico = New DefaultPicOContainer ();
Pico.registerComponentImplement (boy.class);
Pico.registerComponentimplementation (girl.class);
MutablePicoContainer API
Instantiate and use component initialize and use components
Girl Girl = (Girl) Pico.getComponentInstance (Girl.class);
Girl.kisssomene ();
.
The GetComponentInstance method will find the Girl class and match the BOY class and create it and then incorporate the constructor to initialize a Girl instance. First created BOY, then Girl.
PicoContainer API
The Girl Does Not Reach Out To Find Herself A Boy But Instead Is Called The Hollywood Principle Or "Don't call us we'll call you" .Girl class does not call this BOY class It is provided through the container, this is the famous Hollywood rule "Don't find me, I will find you. Introduce an interface for the dependency gives the slave class
. Change the Boy class to implement a Kissable interface and change the Girl class to depend on Kissable instead be a Boy Kissable class implements an interface and changes the reference to a class Girl Kissable objects .public interface Kissable {void kiss (Object kisser);} public Class Boy Implements Kissable {
Public void kiss (Object kisser) {
System.out.println ("I Was Kissed By" Kisser;
}
}
Public class girl {
Kissable kissable;
Public Girl (Kissable Kissable) {
THIS.KISSABLE = kissable;
}
Public void kisssome () {
Kissable.KISS (this);
}
}
Assemble and components just as before use: as before to mounting assembly: MutablePicoContainer pico = new DefaultPicoContainer (); pico.registerComponentImplementation (Boy.class); pico.registerComponentImplementation (Girl.class); Now run:
Run now:
Girl Girl = (Girl) Pico.getComponentInstance (Girl.class);
Girl.kisssomene ();
The Girl Will Be Given A Boy, Because PicoContainer Unders That IT IS A KissableGirl will automatically receive a Boy type because the micro-capacity knows Boy is a Kissable object The Girl and the boy no longer depend on each other, this is caled the Dependency Inversion PrinciPle Since Both Components Depend On The Interface Other.girl and Boy have no dependencies, which is Dependency Inversion PrinciPle, which is dependent on the interface without direct dependencies.
Use Simple Lifecycle Controls with Simple Life Cycle
Change the Girl class to implement the simple default lifecycle and do it's kissing when the container is started. Change Girl class implements the default life cycle control class, and his vessel to start is on the "kissing" public class Girl implements Startable {Kissable in Kissable; Public Girl (kissable kissable) {this.kissable = kissable;} public void start () {kissable.kiss (this);} public void stop ()}}}
Assemble Container AS Before But Instead The Container Like this: Assembly is equipped with a container as before, but does not need to directly call the Girl method, just start the container: Pico.start (); this will instantiate all Components That Implement Startable and Call The Start Method on Each of The Stop and Dispose The Container DO AS FOLLOWS: This method calls all components that implements the startable and call their Start method. Stop them can use the method Pico.stop (); pico.dispose ();
STARTABLE API
Disposable API