Date: 2004-03-04T21:13:49
Editor: 130.89.169.128 <>
Wiki: JicarillaWiki
Page: http://lsd.student.utwente.nl/jicarilla/IocContainerInternals_2fTheBasics_2fTheGlorifiedHashMap
no comment
Change Log:
------------------------------------------------------------------------------
@@ -1,6 +1,33 @@
= The Glorified !HashMap =
''(part of the IocContainerInternals paper)''
+== No container ==
+
+We're going to start with a stupid Swing example that doesn't use a container at all:
+
+{{{
+public class NoSwingContainerAtAll
+{
+ public final static void main( String[] args )
+ {
+ // Setup Stage
+ JButton[] buttons = new Object[10];
+
+ // Assembly Stage
+ for( int i = 0; i < buttons.length; i++ )
+ buttons[i] = new JButton( "Button " + i );
+
+ JFrame frame = new JFrame( "So...many...roses..." );
+ JRootPane pane = frame.getRootPane();
+ for( int i = 0; i < container.length-1; i++ )
+ pane.add( buttons[i] );
+
+ // Initialization Stage
+ ((JFrame)container[container.length-1]).setVisible(true);
+ }
+}
+}}}
+
== The simplest container ==
The simplest container is an array. Here's an example:
@@ -20,7 +47,7 @@
JFrame frame = new JFrame( "Worst episode ever!" );
JRootPane pane = frame.getRootPane();
for( int i = 0; i < container.length-1; i++ )
- pane.add( container[i] );
+ pane.add( (Component)container[i] );
container[container.length-1] = frame;
@@ -30,7 +57,7 @@
}
}}}
-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. Let's move up to a !HashMap...
+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...
{{{
public class HashMapBasedSwingContainer
@@ -77,7 +104,7 @@
// Assembly Stage
for( int i = 0; i < 10; i++ )
{
- JButton button = new JButton( "Button " + i );
+ JButton button = new JButton( "Button " + i ); // My eyes, my beautiful eyes!
container.put( ""+i, button );
}
@@ -89,3 +116,31 @@
----
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:
+
+{{{
+public class IoCButtonContainer
+{
+ public final static void main( String[] args )
+ {
+ // Setup Stage
+ IoCButtonContainer container = new IoCButtonContainerImpl(
+ "You don't make friends with salad! " +
+ "You don't make friends with salad!");
+
+ // Assembly Stage
+ for( int i = 0; i < 10; i++ )
+ container.addJButton( ""+i, "Button " + i ); // It only hurts a little!
+
+ // Initialization Stage
+ container.initialize();
+ }
+}
+}}}
+
+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 IoCButtonContainer does or what its contract is. Imagine having a big real life application, like one with, oh, 1500 buttons of 100 different kinds, all with slightly different configuration. Saving 50% of the lines of code in an application like that seems like a smart idea. Furthermore, we've lost that totally ugly 'new' for all those buttons. You might imagine that this allows all sorts of tricks, optimizations, and code security. We'll save the example of that for a later chapter, but hold on to that thought.
+
+----
+That's all for the night (at least on my part). More tomorrow. G'night.
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.