Creating and Destroying Objects

Objects do not always appear at the top of a UML sequence diagram; they are often constructed by other objects. In this case, a placeholder object is defined at the beginning of the diagram, to leave the appropriate space of the object. Later a create message will actually create an object with the given label. Any object can also receive a destroy message, that will stop its life. The lifeline of destroyed messages is not typically completed.

The following definition extends our previous diagram with a dynamically created flow object.

# Define the objects
object(S,"s:Switch");
object(P,"p:Pump");
placeholder_object(F);
step();
active(S);
active(P);

# Message sequences
message(S,P,"run()");
create_message(P,F,"f:Flow");
active(F);
message(S,P,"stop()");
destroy_message(P,F);

# Object completion
step();
complete(S);
complete(P);

The resultant diagram is
Sequence diagram with object construction and destruiction