[wiki] New: IocContainerInternals/TheBasics/TheGlorifiedHashMap

[email protected]
Newsgroups gmane.comp.java.jicarilla.cvs
Message-ID <[email protected]>
   Date: 2004-03-04T20:58:54
   Editor: 130.89.169.128 <>
   Wiki: JicarillaWiki
   Page: http://lsd.student.utwente.nl/jicarilla/IocContainerInternals_2fTheBasics_2fTheGlorifiedHashMap

   no comment

New Page:

= The Glorified !HashMap =
''(part of the IocContainerInternals paper)''

== The simplest container ==

The simplest container is an array. Here's an example:

{{{
public class ArrayBasedSwingContainer
{
  public final static void main( String[] args )
  {
    // Setup Stage
    Object[] container = new Object[11];

    // Assembly Stage
    for( int i = 0; i < container.length-1; i++ )
      container[i] = new JButton( "Button " + i );

    JFrame frame = new JFrame( "Worst episode ever!" );
    JRootPane pane = frame.getRootPane();
    for( int i = 0; i < container.length-1; i++ )
      pane.add( container[i] );

    container[container.length-1] = frame;

    // Initialization Stage
    ((JFrame)container[container.length-1]).setVisible(true);
  }
}
}}}

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...

{{{
public class HashMapBasedSwingContainer
{
  public final static void main( String[] args )
  {
    // Setup Stage
    Map container = new HashMap();

    // Assembly Stage
    for( int i = 0; i < 10; i++ )
    {
      JButton button = new JButton( "Button " + i );
      container.put( ""+i, button );
    }

    JFrame frame = new JFrame( "Worst episode ever!" );   // D

    JRootPane pane = frame.getRootPane();                 // '
    for( int i = 0; i < container.length-1; i++ )         // o
      pane.add( container[i] );                           // h

    container.put( "frame", frame );                      // !

    // Initialization Stage
    container = Collections.unmodifiableMap( container ); // !
    ((JFrame)container.get("frame")).setVisible(true);    // !
  }
}
}}}

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

{{{
public class SmartHashMapBasedSwingContainer
{
  public final static void main( String[] args )
  {
    // Setup Stage
    SmartHashMap container = new SmartHashMapImpl(
      "You don't make friends with salad! " +
      "You don't make friends with salad!");

    // Assembly Stage
    for( int i = 0; i < 10; i++ )
    {
      JButton button = new JButton( "Button " + i );
      container.put( ""+i, button );
    }

    // Initialization Stage
    container.initialize();
  }
}
}}}

----
Okay, let's take a break. You can buy me a Duff.


-------------------------------------------------------
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.