Dynamically Creating and Accessing a Bean Instance

VisualAge provides beans that enable you to dynamically create and reference bean instances visually. A Factory creates new instances of a bean type, based on a connection from an event to a constructor for the Factory's type. A Variable references any instance of the Variable's type that you assign to it using a connection. With either a Factory or a Variable, you specify the bean type that it can create or reference.

A Factory's type specifies the type of bean instance, or object, that it creates. A Variable's type specifies the type of object that can be assigned to it. For example, if you change a Factory's type to Customer, it can create Customer objects. If you change a Variable's type to Customer, you can use it to reference any Customer object that you assign to it.

You can visually create and access beans in the Visual Composition Editor.

  1. Add one of the following beans and specify the type of bean it represents:


    Bean Description
    Factory A bean that dynamically creates instances of Java beans
    Variable A bean that provides access to instances of Java beans

    Adding a Factory or Variable bean

    1. Select a Factory or Variable bean from the Other category of the beans palette. Alternatively, you can select a class type as a Variable in the Choose Bean window.

    2. Add the bean to the free-form surface of the composite bean you are composing.

    Changing the Factory or Variable type
    When you add a Factory or Variable bean from the palette, its initial type is Object. Change the type as follows:
    1. Open the pop-up menu of the bean.
    2. Select Change type to open the Choose a Type window.
    3. Enter the full or partial type name in the Pattern field.
    4. Select the type you want in the Class Names or Type Names field.
    5. If the type is found in more than one package, select the package in the Package Names field that contains the one you want.
    6. Select OK.

    After you change the type, you can make connections to features of the new type.

    Changing the bean name
    You might want to change the name of a Factory or Variable bean to a more descriptive name for visual composition. This can be particularly helpful for a Variable in some circumstances:
    • If you change the Variable type. The initial name of the Variable reflects its initial type.
    • If you promote a feature from a Variable to a composite bean. The Variable name is reflected in the default name of the promoted feature.

    Change the name as follows:

    1. Open the pop-up menu of the Factory or Variable bean.
    2. Select Change Bean Name to open the Bean Name Change Request window.
    3. Enter the new name.
    4. Select OK.

  2. Define when and how how the Factory or Variable bean is used.

    Creating objects with a Factory
    Connect an event to a Factory constructor method. If the constructor you choose requires parameter values, provide these values either with additional connections or property settings. Because the Factory references an object that it creates until it creates another object, you can make connections from its this event to methods and properties of the object it references.

    Assigning a bean instance to a Variable
    Connect a bean property of the same type as the Variable to the this property of the Variable. The connection assigns the source property to the Variable, so the Variable references the source as a bean instance. If the source bean itself is the source property , use its this property as the connection source.

    Two customized variations of this procedure are commonly used:

    Tearing off a property
    You can gain access to features of a bean that is a property of another bean by detaching a reference to the property as a bean instance in a Variable. Then, you can access the features of the property through the Variable. This procedure is called tearing off a property.

    For example, you might use a Customer bean that has name, address, and phone properties. The address property is an Address bean that has street, city, state, and zipCode properties. When you add a Customer bean, you can make connections to its address property, but not to individual elements of the address. If you tear off the address property of the Customer bean, an Address Variable is placed on the free-form surface. A connection assigns the address property of the Customer bean to the Address Variable. You can make connections to the properties of the Address Variable to access elements of the Customer's address property.

    Promoting a Variable
    You can enhance bean reusability by defining its data source as a property of the bean. When you use the bean in another bean, you can assign the data using a connection to the data property. To do this, add a Variable for the data source bean type in the reusable bean. Then, promote the Variable's this property to the interface of the reusable bean.

    For example, you might compose a CustomerView bean that provides a panel of fields to display or obtain information for a Customer bean. To make the CustomerView bean reusable wherever you might use a Customer bean, you don't want to specify a particular Customer bean as the data model for the CustomerView bean. You can accomplish this as follows:

    1. Use a Customer Variable bean, instead of a Customer bean, in the CustomerView bean as the data model for the customer information fields.

    2. Connect properties of the Customer Variable to corresponding customer information fields to tie the data model to the user interface.

    3. Promote the Customer Variable to the CustomerView bean interface as a customer property.

    4. Whenever you add a CustomerView bean to another bean, also add either a Customer bean or another bean that contains a Customer bean as a property. Connect the Customer bean to the customer property of the CustomerView bean. This assigns the Customer bean to the Customer Variable in the CustomerView bean.

For examples that use a Variable, see the Amortization class in the com.ibm.ivj.examples.vc.swing.mortgageamortizer package, and the AddressView and CustomerView classes in the com.ibm.ivj.examples.vc.customerinfo package. These examples are shipped in the IBM Java Examples project.


Related procedures
Working with Beans Visually
Composing Beans Visually
Promotion of Bean Features
Tearing Off Properties
Using VisualAge Beans in Visual Composition
Adding the IBM Java Examples project

Related references
Factory and Variable Beans
CustomerInfo Sample