Glamor "little girl" --PicoContainer

zhaozj2021-02-08  475

Jon Tirten said in his WebLog, he saw a fun project called PicoContainer and made it out immediately. What is the attractive charm of this self-proclaimed micro-capacity of "IOC (Inversion Of Control) Type 3?

PicoContainer / Nanocontainer I've Recently Joined Two New Projects: http://www.picocontainer.orghttp://www.nanocontainer.org

. The founders of the project are Paul (AltRMI, Enterprise Object Broker) and Aslak (XDoclet, MiddleGen) Actually they pair-programmed most of it at Paul's place and a lot of beer was involved The end result:. A neat, simplistic and Wonderfully TDDED Piece of Work. Joe (Sitemesh, QDox), My Unit-Testing Guru, IS Also in on IT.

It's basically an Inversion-of-Control-container / framework / micro-kernel. Pico will be the simplistic micro-kernel and Nano will be a bunch of containers serving different purposes (most built on top of Pico).

I'm Not an IOC-EXPERT BY Any Means, And, Well, I Didn't Know Much About It Before Chatting with Paul and Aslak. The Cool (at Least By Default) Implements Style 3 IOC, Which Means Constructors Are Used to Define Dependencies. Smart!

I will implement some Nanning support in Nano so that aspects can define dependencies on services and the container will resolve them properly, the aspects will also be able to aspectify the components transparently. The details are far from finalized, just a bunch of semi-digested Ideas. I'll Give You a Couple of Use-Cases Though. An Aspect Implementing Transparent Persistence with Prevayler Could Retrieve It's Prevayler-Instance Just by Declaring It ITS Constructor:

Public class prevayleraspect {

Public prevayleraspect (prevayler prevayler) {/ * ... * /}

/ * ... * /

}

A Declarative Transaction Aspect Could Declare It's Dependency ON A TransactionManager by: Public Class Transactionspect {

Public TransactionSpect (TransactionManager TransactionManager) {/ * ... * /}

/ * ... * /

}

PUT SESESPECTS Along with Their Services in A Container, Pico Does It's Work and All Components Are Properly Assembled:

Picocontainer pico = new hierarchicalPicocontainer.default ();

Pico.registerComponent (RemoteTransactionManagerImpl.class);

Pico.registerComponent (Picoprevayler.class);

Pico.registerComponent (Prevayleraspect.class);

Pico.registerComponent (TransactionAmect.class);

Pico.start ();

Neat and Simple. No XML, No Runtime Attributes, No Fuss.

Another Great to Mock-Test The Things. Say you want to mottle:

MockTransactionManager MockTransactionManager = New MockTransactionManager ();

// ... set up expertations and so forth ...

Transactionaspect Transactionaspect = New TransactionAspect (MockTransactionManager);

AspectSystem aspectsystem = new aspectSystem ();

AspectSystem.Addaspect (TransactionAspect);

TestObject TestObject = (TestObject) aspectSystem.newinstance (TestObject.class);

// ... Run Your Tests on your testObject ...

MockTransactionManager.Verify ();

Check it, You'll Like it! (2003-06-20 11: 25: 47.0)

Coopeously, my other idol Rickard Oberg also looks at this project. He introduced Picocontainer and IOC on his own WebLog, and he was argued with this. Where is this gadget value? I trust Oberg's view.

Why IoC? From the comments on the PicoContainer entry it seems like people are not quite getting what's so great about IoC (Inversion of Control). "If all it does is lookup, what's the point?". To me this question is like saying "What's the point with aircraft? The only thing they do is fly", but I'll give you an example that might highlight why IoC is nice to have. Let's say you have a component A that uses a component B to do something. In The First Iteration of Your Application Both Are Just Pojo's Registered In a Picocontainer. Here's the code for a: public class a {

B comm;

Public A (B CoMP)

{

This.Comp = CoMP;

}

Public String HelloWorld ()

{

Return "Hello" Comp.World ();

}

}

PicoContainer Will, USING IOC, GET A Reference To B And Hand It To A Say That We Now Change So That B Is A Jini Service That Is Located Using Jini Service That Is Located Using Jini Discovery. The Code for a Now Looks Like this : Public Class A

{

B comm;

Public A (B CoMP)

{

This.Comp = CoMP;

}

Public String HelloWorld ()

{

Return "Hello" Comp.World ();

}

}

Looks familiar, does not it? But, Jini is not all that popular these days so we changed B into a webservice and put some failover on top of it using grid computing. With all of this new high-tech stuff running the show , the code for a now look loocks like this: public class a

{

B comm;

Public A (B CoMP)

{

This.Comp = CoMP;

}

Public String HelloWorld ()

{

Return "Hello" Comp.World ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-749.html

New Post(0)