Upload
annabel-dalton
View
230
Download
1
Tags:
Embed Size (px)
Citation preview
Associations
“a relationship between instances of classes (objects) that indicates some meaningful and interesting connection” (text, p. 150)
Ins tructor CourseSectionteaches
When to show Associations
When “memory” of a relationship is required (in a real world situation, not a software situation) SalesLineItem instances are associated with a Sale
instance. Otherwise, it would not be possible to reconstruct a sale, print a receipt, or calculate a sale total.
Finding Associations
“Need to remember”“Preserved for some duration”Common Associations List (p.155)
“patterns” – e.g. Sale to SaleLineItem to Item.
Multiplicity
Multiplicity defines how many instances of class A can be associated with one instance of class B
Association Details
NameMultiplicity (of Roles)Multiple AssociationsNOTE: Multiplicity depends on time
Examples p.157
CourseSectionInstructor
0..n1 0..n1
Teaches
Fig. 9.12 (p.152)
SaleRegister Records-current 0..11
association name multiplicity
-"reading direction arrow"-it has no meaning except to indicate direction of reading the association label-often excluded
Object Attributes & Multiplicity
If we are selling a quantity of n of the same product (product:Product) then the SaleLine is associated with quantity n of that Product. But it is only associated with 1 Product.
Note: this is sometimes modeled as a many to many association in a conceptual domain model to refer to the fact that a sale line can refer to many of the same product (e.g. 4 green peppers) BUT we are beyond that—as soon as we specify that product refers to an instance of the Product class, the multiplicity on the Product end should be a 1.
Sale
saleNumberdatesaleLineSet : SaleLine
SaleLine
product : Productquantity
1..*
1
1..*
1
Product
prodNumber
10..*0..* 1
Sells quantity of
Contains
A good association name helps clarify the association
Guidelines
In the domain Model Relate conceptual classes with an association, NOT an
attribute Cashier to Register NOT currentRegister attribute of Cashier
Fig. 9.22 (p.165)
Cashier
namecurrentRegister
Cashier
name
Register
number
Uses
Worse
Better
not a "data type" attribute
1 1
Ways to Show UML Attributes: Attribute Text and Association Lines
Attributes are shown several ways: attribute text notation, such as currentSale : Sale. association line notation both together
Figure 16.3 shows these notations being used to indicate that a Register object has an attribute (a reference to) one Sale object.
Directional Associations: CAUTION
NOTE: the information that follows is only for the purposes of answering the question “when would we use directional associations?” Directional associations have arrows; regular
associations do not
Directional Associations?
Directional associations are typically part of a Design Model (further on in the domain/design process); we would not usually show this in a domain model. (Larman, p.253)
What this means:• Order “knows” about OrderLine; OrderLine does not “know” about Order. In code, each Order class contains a reference to OrderLine objects (productOrderedSet). The OrderLine objects do not contain a reference to OrderLine.
OrderLine
product : Productquantity
1..*
contains
1
Order
orderIDcustomer : CustomerproductOrderedSet : OrderLine
Other Examples
Address
streetNumstreetNamecitypostcodeprovincecountry
Customer
custIDcustNamecustAddress : AddresscontactSet : ContactisInactive
1
1
1
1operates at
Contact
contactIDcontactNamecontactPhone
1..*
1
1..*
1
can be reached by calling
public class Customer {private int custID;private String custName;private Address address;private List<Contact> contactSet;private Boolean isActive;
public class Address {private int streetNum;private String streetName;private String city;private String postCode;private String province;private String country;
public class Contact {private int contactID;private String name;private String phone;
Review: Data types
A data type refers to objects for which unique identity is not important.
Common data types are primitive-oriented types such as: Boolean, Date (or DateTime), Number, Character,
String (Text), Time, Address, Color, Geometrics (Point, Rectangle), Phone Number, Social Security Number, Universal Product Code (UPC), SKU, ZIP or postal codes, enumerated types
Object Attributes: Where & When?
Sale
saleNumberdatesaleLineSet : SaleLine
SaleLine
product : Productquantity
1..*
1
1..*
1
Product
prodNumber
10..*0..* 1
Showing objects as attributes is typically reserved for the Design model (further on in the analysis and design process) but we are showing them here in SYS466 to aid in understanding.
Sale
saleNumberdatesaleLineSet : SaleLine
SaleLine
product : Productquantity
1..*
1
1..*
1
Product
prodNumber
10..*0..* 1
Object Attributes: References
In OO language implementation, objects will contain references to their object attributes. For example SaleLine will contain a reference to a Product object.
Example of Relationship between Order to OrderLine to Product
public class Order {
private int orderID;private Customer customer;private List<OrderLine>
productOrderedSet;
public class OrderLine {
private int qtyOrdered;private Product product;
public class Product {
private int productID;private String productName;
Product
productIDproductName
OrderLine
product : ProductqtyOrdered
1
1..*
1
1..*
orders
1..*
1
1..*
Order
orderIDcustomer : CustomerproductOrderedSet : OrderLine
orders quantity of
contains
When to Use Attribute Text versus Association Lines for Attributes
Guideline: Use the attribute text notation for data type objects and the association line notation for others. Both are semantically equal, but showing an association line to another class in the diagram (as in Figure 16.3) gives visual emphasis—it catches the eye, emphasizing the connection between the class of objects on the diagram.