« Multiple Constructor Parameters of the Same Type in PicoContainer | Main | Critique of XpTrackerPlugin for TWiki »

Specifying Instances of Multiple Types in PicoContainer

Having satisfied myself yesterday on the question of whether or not PicoContainer can pick specific object instances of the same type out of the container while building other components, I now turn to the question of whether it can perform with the same precision while working with objects of differing type. I am happy to find that it is indeed possible and simple.

Let's say that the example I've been using so far, involving the Juice object, has an optional third constructor, which accepts two Fruit objects and a Cup object. I need to distinguish between specific instances of Fruit, and add a different type as the third constructor parameter. Here's how you accomplish that in PicoContainer.

    public void testMultiplePicoParamTypes() throws Exception {
        addFruit("lemon");
        addFruit("lime");
        addFruit("bubble", "tapioca");
        c.registerComponentInstance(
                "teacup", 
                new Cup("teacup"));

        c.registerComponentImplementation(
                "lemonbubbletea",
                Juice.class, 
                new Parameter[]{
                    new BasicComponentParameter("lemon"),
                    new BasicComponentParameter("bubble"),
                    new BasicComponentParameter("teacup")});

        Juice lemonbubbletea = getJuice("lemonbubbletea");

        Assert.assertEquals(
                "lemon", 
                lemonbubbletea.getType());

        Assert.assertEquals(
                "tapioca", 
                lemonbubbletea.getSecondIngredient());

        Assert.assertEquals(
                "teacup", 
                lemonbubbletea.getCupType());

        claimVictory();
    }

This permits one to keep a broad level of abstraction for the object instances, while maintaining specifically how they are assigned to be used by another object later on. Soon, I will illustrate why this is useful and in fact crucial to certain types of programming.

TrackBack

TrackBack URL for this entry:
http://blog.lib.umn.edu/cgi-bin/mt-tb.cgi/9110

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)