BTS430 Lab: Aggregation, Composition, Inheritance and Sequence Diagram 1 Case Study: A Library system for checking out library items. Below is the PARTIAL scenario for Checkout Library Book (from the Check Out Library Item use case) Actor (librarian) Starts the checkout process Scans a book System Creates a loan record Creates a loan item record for the library book Below is the PARTIAL domain class diagram for the Checkout Library Item use case. Loan 1 1..n borrows LoanLineItem 0..1 LibraryCD LibraryDVD LibraryBook 1 LibraryMagazine In this Tutorial: 1. Loan is a composite of LoanLineItem. You will model the composite relationship 2. LibraryItem would be a good generalization of LibraryBook, LibraryCD and LibraryDVD and LibraryMagazine. You will create a parent class titled LibraryItem and you will establish it as the parent class with children LibraryBook, LibraryCD, LibraryDVD and LibraryMagazine inheriting from it. 3. You will create a sequence diagram that models the Checkout Library Book scenario above. BTS430 Lab: Aggregation, Composition, Inheritance and Sequence Diagram 2 Model The Composite Relationship Between Loan and LoanLineItem: 1. Open up the Rational Rose diagram (on the BTS430 schedule). It contains most of the classes you will need. Navigate to the logical view Select the Library Classes package Add a class diagram to the package and name it Composition. 2. Double click on your class diagram to make sure it is the active diagram. Drag the following classes into the diagram: Loan LoanLineItem LibraryBook 3. Create an association between LoanLineItem and LibraryBook. Set up multiplicity so that a LoanLineItem is related to 1 LibraryBook and LibraryBook is related to 1 or 0 LoanLineItem. Name the association “borrows”. 4. Now you are going to draw the composite between Loan and LoanLineItem. Select the Aggregation tool from the tool bar. If it is not in the toolbar you must add it by right clicking on the toolbar, selecting customize and then selecting the aggregation tool. When you select the aggregation tool the cursor will become an arrow. Click on the container class—Loan--and then hold the mouse button down and drag to the contained class—LoanLineItem. You should see an association with an open diamond at the Loan end. Name the association something like “consists of”. Double click on that association. The Aggregation Specification window should appear. Click on the “RoleA Detail tab”. If the Aggregate checkbox is not checked and Aggregate is grayed out, then click on the “RoleB Detail” tab. When you see the Aggregate box checked, select the “by value” radio button below the aggregate checkbox. Click on OK and you should see a correct composite relationship with the diamond filled in. 5. Now add multiplicity to the Loan, LoanLineItem relationship. Each Loan is related to 1 or more LoanLineItems and each LoanLineItem is related to 1 Loan. You have used composition to create a very common business structure. This is the same structure as Sale to SaleItem, with SaleItem related to a specific product. Your structure should look like this: Loan 1 consists of 1..n borrows LoanLineItem 0..1 LibraryBook 1 BTS430 Lab: Aggregation, Composition, Inheritance and Sequence Diagram 3 Create an Inheritance Relationship 1. Create an inheritance class diagram. Click on the Library Classes package in the logical view Add a class diagram, name it Inheritance 2. Double click on your class diagram to make sure it is the active diagram. In the diagram create a LibraryItem class. 3. Drag the following classes into the same diagram: LibraryBook LibraryCD LibraryDVD LibraryMagazine 4. Add the following attributes to LibraryItem: libID title 5. Add the following functions to LibraryItem: getTitle() removeFromCirculation() 6. LibraryItem is the base or parent class, the other classes are derived or child classes. 7. Create an inheritance relationship: Select the Generalization tool from the tool bar (line with arrow that is not filled in). If it is not in the toolbar you must add it by right clicking on the toolbar, selecting customize and then selecting the generalization tool. When you select the generalization tool the cursor will become an arrow. Click on one of the child classes—LibraryBook--and then hold the mouse button down and drag to the parent class—LibraryItem. You should see an association with an the generalization arrow at the parent end. Repeat this step until all children are associated with the parent. You will have 4 arrows pointing to the parent. 8. To make the set of arrows “tidier”, select one of the arrows and drag it so that it touches the line portion of another of the child to parent relationships. They will be combined—continue until all are combined. Your diagram should be similar to the following: BTS430 Lab: Aggregation, Composition, Inheritance and Sequence Diagram 4 9. Make the parent class abstract: Double click on LibraryItem so that the class specification is displayed. In the Stereotype field, type in “Abstract” Click on the detail tab Check the abstract box. This will put the class name into italics. Click OK The class should now have an Abstract stereotype and the name should be in italics. 10. Make the parent’s attributes protected: You want to change the attributes of the parent from private (the default) to protected so that each child has access to them. Double click on LibraryItem so that the class specification is displayed. Select the Attributes tab. Double click the fist attribute—the attribute specification window will pop up. Under “Export Control” click on the Protected radio button. Click OK. Repeat for all attributes Note that the icon beside the attribute names in the parent will have changed to denote protected instead of private. Now double click on any child class and select the Attributes tab; you will see the parent attributes there. If you select the Operations tab you will see the parent’s operations. Operations default to public so you don’t have to change their visibility within the parent. Create a Sequence Diagram for the Check Out Library Book Scenario 1. Create an empty sequence diagram. Click on the Library Classes package in the logical view Right click, select new, then Sequence Diagram. A sequence diagram will be created. Name it CheckOutBook Double click on it so that an empty sequence diagram is displayed in the right window. 2. Turn “Focus of Control off: From the rational rose menu bar select tools, then options. An options window will appear. Select the diagram tab. Find the “focus of control” checkbox (in the list on the right). If it is checked, de-select it and click on OK. 3. Add an Actor to the sequence diagram. Select the librarian actor from the use case view and drag it into your diagram. 4. Add a control object Select the CheckoutHandler class from the left window and drag it into the diagram. A :CheckoutHandler object will appear. 5. Add a message from the librarian actor to the handler. Click on the “object message” icon (a straight arrow). BTS430 Lab: Aggregation, Composition, Inheritance and Sequence Diagram 5 Click on the actor’s life line, hold the mouse button down and drag to the :CheckoutHandler’s life line. Release. While the line is still selected, key in the name of the message: “startCheckout()”. You can change the name of any message by double clicking on the message and keying its name into the message specification window that appears. 6. Next, drag a :Loan object into your diagram so that it is to the right of the :CheckoutHandler. 7. Add a message titled “create()” from :CheckoutHandler to :Loan. That ends the first row in the scenario. 8. Now model the second row: Add a message titled “checkoutItem(itemID)” from the Librarian to the :CheckoutHandler. Add a message titled “addLineItem(itemID)” from the CheckoutHandler to the Loan Now drag a LoanLineItem object into the diagram. Make sure it is to the right of Loan. Add a message titled “create()” from :Loan to :LoanLineItem. That ends the second (and final) row in the scenario. Your sequence diagram should look like this: : Librarian : CheckoutHandler : Loan : LoanLineItem startCheckout() create() checkout(itemID) addLineItem(itemID) create()