Implementation Model: Mapping Designs to Code Ch. 20
Defining a Class with Methods and Simple Attributes public class SalesLineItem { private int quantity; public SalesLineItem(ProductSpecification spec, int, qty) {...} public Money getSubtotal() {…} … } ProductSpecification description : Text price : Money itemID : ItemID SalesLineItem * Described-by 1 quantity : Integer getSubtotal():Money
Adding Reference Attributes public class SalesLineItem { private int quantity; private ProductSpecification productSpec; // reference attribute … } ProductSpecification description : Text price : Money itemID : ItemID SalesLineItem * Described-by 1 quantity : Integer getSubtotal():Money
Reference Attributes and Role Names public class SalesLineItem { private int quantity; private ProductSpecification productSpec; … } ProductSpecification description : Text price : Money itemID : ItemID SalesLineItem * Described-by 1 quantity : Integer productSpec getSubtotal():Money
Creating Methods from Interaction Diagrams addLineItem(itemID, quantity) 2 : makeLineItem(spec, quantity) :Register 1:spec := getSpecification(itemID) :Sale :ProductCatalog 1.1:spec := find(itemID) 2.2 : add(sli) 2.1 : create(spec, quantity) :ProductSpecification sli:SalesLineItem :SalesLineItem
The Register – addLineItem method The addLineItem collaboration diagram will be used to illustrate the Java definition of the addLineItem method. In which class does addLineItem belong to? The addLineItem message is sent to a Register instance, therefore the addLineItem method is defined in class Register. public void addLineItem(itemID itemID, int quantity); Message 1. A getSpecification message is sent to the productCatalog to retrieve a productSpecification productSpecification spec = catalog.getSpecification(itemID); Message 2: The makeLineItem message is sent to the Sale. sale.makeLineItem(spec, quantity);
A Skeletal definition of Register Class public class Register { private productCatalog catalog; private Sale sale; public Register (ProductCatalog pc) {…} public void endSale(); public void addLineItem (ItemID itemID, int quantity); public void makeNewSale() {…} public void makePayment (Money cashTendered) {…} } ProductCatalog getSpecification() 1 Looks-in 1 Sale becomeComplete() makeLineItem() makePayment() getTotal() Date : Date isComplete : Boolean time : Time Register Captures 1 1 endSale() addLineItem() makeNewSale() makePayment()
Container/Collection Classes in Code Sale Date : Date isComplete : Boolean time : Time SalesLineItem Contains quantity : Integer 1 1 .. * getSubtotal():Money becomeComplete() makeLineItem() makePayment() getTotal() A container class is necessary to maintain attribute visibility to all the SalesLineItem instances. Vector is an example of a dynamic data structure. public class Sale { . . . private Vector lineItems; }