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.


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

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.

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 bean pop-up menu.
  2. Select Change type to open the Choose a Type window.
  3. In the Pattern field, enter the type name.
  4. In the Class Names or Type Names field, select the type you want.
  5. In the Package Names field select the desired package.
  6. Select OK.

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

Creating objects with a Factory

Connect an event to a Factory constructor method. If the constructor you choose requires parameter values, you can provide these values with additional connections or with property settings. Because the Factory references an object that it creates, you can make connections from the Factory'sthis event to methods and properties of the referenced object.

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. This connection assigns the source property to the Variable, and the Variable references the source as a bean instance. If the source bean is the source property, use its this property as the connection source.

You can use two customized variations of this procedure:

  1. Tearing off a property. If a bean is a property of another bean, you can access its features by creating a bean instance of the property in the form of a Variable. You accomplish this by tearing off the property of the bean. You can then access the features of the property through the Variable. For example:
  2. 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:

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
Visual bean basics
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