Links between objects are created and deleted over time. There are several approaches to modeling associations. We can
13.1.1 Associations without Explicit LifecyclesSome associations simply exist. In the online bookstore, new products are made available for sale and some products are discontinued. Each product is published by some Publisher. That means that whenever a new product is added, there must be a publisher for that product, as shown in Figure 13.1. Figure 13.1. Class Diagram for Publisher and ProductThe association multiplicity specifies the rules for what must happen when instances of the classes are created and deleted. For example, whenever a product is created or deleted, a link for the Publisher PRODUCES AND MARKETS Product association must be correspondingly created or deleted. If a Publisher is deleted, all the products published by that publisher must be deleted as well. 13.1.2 Dynamic Associations with Association ClassesOther associations progress through distinct stages during which different rules and policies apply. These associations are modeled using an association class with a state machine. The state machine prescribes the behavior of a typical unspecified link. Each link executes this same state machine, and, just as with a class, we use the term state machine instance to refer to the execution of the state machine by a particular instance of the association. The Product Selection association class in the online bookstore, shown in Figure 13.2, has a statechart diagram that receives signals to add a new selection, to change the quantity of an existing selection, and to remove the selection from the shopping cart. Figure 13.2. Class Diagram for Product SelectionThe statechart diagram for the Product Selection is shown in Figure 13.3. Figure 13.3. Statechart Diagram for Product SelectionSince a Shopping Cart must have at least one Product (multiplicity 1..*), when a Product Selection is deleted it checks to see if its deletion would result in an empty cart; if the selection being deleted is the last one in the cart, it signals the cart to delete itself, thereby maintaining the integrity of the association. The coordination between the classes and the online customer external entity is shown in the collaboration diagram in Figure 13.4. Figure 13.4. Collaboration between Product Selection and Order |