Picocontainer-two minute tutorial

Source: Internet
Author: User
From: http://www.picocontainer.org/Two+minute+tutorial

Two minute tutorial
Two-minute tutorial
 

Authors:Jon tirsen

This very short tutorial shocould get you up to speed with picocontainer in 2 minutes. It does not go into why you shocould do it, readFive minute IntroductionFor that.
This is a very short two-minute tutorial on how you can quickly learn about micro-containers. If it still cannot be determined why you want to use micro-containers, you can continue to read the 5-minute introduction.

Download and install

DownloadThe jar file and include it in your classpath.

Write two simple components
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 kissSomeone() {        boy.kiss(this);    }}
Assemble Components
Assemble Components

MutablePicoContainer pico = new DefaultPicoContainer();pico.registerComponentImplementation(Boy.class);pico.registerComponentImplementation(Girl.class);

 Mutablepicocontainer API

Instantiate and use component
Initialize and use components

Girl girl = (Girl) pico.getComponentInstance(Girl.class);girl.kissSomeone();

Getcomponentinstance will look at the girl class and determine that it needs to create a boy instance and pass that into the constructor to create a girl. The boy is created and then the girl.
The getcomponentinstance method searches for the girl class and matches the boy class, creates it, and passes in the constructor to initialize a girl instance. The first boy is created and then the girl.

 Picocontainer API

The girl does not reach out to find herself a boy but instead is provided one by the container. This is calledHollywood PrincipleOr "don't call us we'll call you ".
The girl class does not call this boy class by itself but is provided through containers. This is the famous Hollywood rule "don't look for me, I will look for you".

Introduce an interface for the dependency
An interface for the subordinate class

Change the boy class to implement a kissable interface and change the girl class to depend on kissable instead.
Implement a kissable interface for the boy class and change the girl class to reference A kissable object.

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 kissSomeone() {        kissable.kiss(this);    }}

Assemble and use components just as before:
Assemble components as before:

Mutablepicocontainer Pico = new defaultpicocontainer ();
Pico. registercomponentimplementation (boy. Class );
Pico. registercomponentimplementation (girl. Class );

Now run:
Run now:

Girl girl = (girl) Pico. getcomponentinstance (girl. Class );
Girl. kisssomeone ();

The girl will be given a boy, because picocontainer understands that it is a kissable
Girl automatically receives a boy type, because the micro-container knows that boy is an kissable object.

The girl and the boy no longer depend on each other, this is calledDependency inversion principleSince both components depend on the interface and no longer directly on each other.
There is no dependency between girl and boy.Dependency inversion principle, That is, the dependency interface does not directly exist.

Use simple Lifecycle
Use simple lifecycle Control

Change the girl class to implement the simple default lifecycle and do it's kissing when the container is started.
Change the girl class to implement the default lifecycle control class, and enable the "kissing" class when the container is started.

Public class girl implements startable {
Kissable;

Public girl (kissable ){
This. kissable = kissable;
}

Public void start (){
Kissable. Kiss (this );
}

Public void stop (){
}
}

Assemble container as before but instead of calling the girl directly just start the container like this:
Container assembly is the same as before, but you do not need to call the girl method directly. You only need to start the container:

Pico. Start ();

This will instantiateAllComponents that implement startable and call the start method on each of them. To stop and dispose the container do as follows:
This method will call all components that implement startable and call their start method. You can use the following method to stop them:

Pico. Stop ();
Pico. Dispose ();

Startable API

Disposable API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.