We're going to start with a stupid Swing example that doesn't use a container at all:
The simplest container is an array. Here's an example:
in this example, the array that's aptly dubbed "container" is a container for a set of swing components. The added value of using a container instead of just wiring the components together manually is probably negative. I hope you agree the above sample makes even less sense than the first one. Bear with me please. Let's move up to a HashMap...
seems like no just about no gain whatsoever. The trick now is that we're going to make our container more intelligent (we'll be making it less general at the same time). I want a container that automatically provides me with a JFrame (you can call the JFrame a "container facility" if you're into fancy terms) automatically, and that understands to add any button I feed it to that frame. The need for such a thing is highly debatable, but so is following a large amount of floating donuts and falling down a steep cliff doing so. While I'm at it, lets make the container worry about what exactly goes on during "initialization" as well. In other words, make the container handle the "D'oh!!!". We now get something like
------------------------------------------------------------------------------
Okay, let's take a break. You can buy me a Duff.
There's one more thing we should do here to complete the picture: the container needs to be in charge of the "new" keyword. Let us put the "I" in "IoC". It goes a little something like this:
this example is now 4 lines shorter (50%!) than the original one that didn't use a container at all. It may be a little less apparent immediately exactly what is going on, but that's just because we don't actually know what IoC
------------------------------------------------------------------------------
That's all for the night (at least on my part). More tomorrow. G'night.