10.2 Creating and Deleting Objects

Objects"-->

Many classes have instances that are created and deleted over time. Actions can synchronously create and delete instances of other classes without state machines, as shown on these states of the Order in Figure 10.4.

Figure 10.4. Order Creates and Deletes Instances of Selection

graphics/10fig04.gif

The instances of Selection are created and deleted synchronously as part of the procedure. Likewise, the associations between the Order and the Selection are created and deleted directly in the procedures.

10.2.1 Asynchronous Creation and Deletion

Objects can be created and deleted asynchronously by sending signals. A generate signal action to <class> creator performs this function:


generate requestShipment (order: self) to Shipment creator;

Figure 10.5 shows how an Order creates a Shipment once its payment is approved.

Figure 10.5. Order State Procedure Signals to Create Shipment

graphics/10fig05.gif

Executing the action language to Shipment creator; causes the new Shipment object to be created. The signal requestShipment then causes the transition from the initial pseudostate into the Creating Shipment state, as shown in Figure 10.6. The event parameters are delivered to the procedure and the procedure is executed on the new, object that now exists.

Figure 10.6. New Shipment is Created, Enters its Initial State, and Executes its Procedure

graphics/10fig06.gif

Signaling to creator when there is no transition from the initial pseudostate on that event makes no sense: It just cannot be done.

To delete an object, send a signal like any other. If a transition is made into a state with a transition to a final pseudostate, the final transition will be made on completion of the state's procedure, and the object will be gone.

10.2.2 Synchronous Creation and Deletion

Figure 10.4 showed how procedures can synchronously create and delete instances of classes without state machines.

Synchronous creation and deletion can also be used with care! when the target object has a state machine. Synchronous creation is realized using the ordinary create object action:


create object instance R2D2 of Robot in state 'Idle';

The effect of this action is to create an instance of Robot in the specified state, and, once created, the state machine instance can accept events. The procedure for the initial state is not executed, because there was no transition into the state for this execution.

Similarly, an action can cause the deletion of an object and its associated state machine instance, no matter which state it is in:


select one shipment related by self->Shipment[R3];
delete object instance shipment;

As with initial states, the procedure associated with the state-in-which-the object-is-deleted is not executed. Consequently, it is the responsibility of the developer to remove all links to the about-to-be-deleted object and carry out any other necessary cleanup.

Use and Misuse of Synchronous Creation and Deletion

While it is good practice to localize behavior in the class that owns the instance, it is not always best practice. Consider the case in which a customer is created, one or more accounts go through their lifecycles until the last account is closed, and then the customer is deleted. The lifecycle of the customer is boring beyond belief: It's created, it passes information along to the account to cause its creation, and then it goes away. All the processing is under the control of the account.

A more effective and less error-prone method is simply to create and delete the customer in the account class. However, do localize the use of this feature to a single creator and deletor wherever possible.



Executable UML. A Foundation for Model-Driven Architecture
Executable UML: A Foundation for Model-Driven Architecture
ISBN: 0201748045
EAN: 2147483647
Year: 2001
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net