View
216
Download
0
Category
Preview:
Citation preview
Kate Gregory
Week 5
• Static Object Model– Attributes– Methods – Associations– Visibility
• Lab 3 available– Rating your group
See Me
• Send me email if you did not hand in Lab 1 or 2– Include your full name– Tell me what group you have joined
• Hands up now– if you are not in a group– if you are in a group of 1 or 2
Kate Gregory 2
Date Week Topic Hand Out Due Back Test
6-Sep-13 1 Administrivia / Overview / Motivation, benefits of OO
13-Sep-13 2 Use Cases Lab 1: Use cases
20-Sep-13 3 CRC Cards, collab graphs Lab 2: CRC cards lab 1 5%
27-Sep-134 start class diag lab 2 5%
4-Oct-13 5 Finish class diag, Associations Lab 3: Class Diag
11-Oct-13 6 Inh & Polymorphism / midterm review lab 3 5%
18-Oct-13 7 midterm Midterm 25%
25-Oct-13 Reading Break
1-Nov-13 8 Interaction diag / Design Patterns Lab 4: Interaction Diag
8-Nov-13 9 Good Design / Modules & Packages / Deployment and component diagrams /Metrics / SOLID
Lab 5: Critiques lab 4 5%
15-Nov-13 10 State diagrams / Activity diagrams / Summary and Conclusion / The Future
22-Nov-13 11 Critiques critique lab (before class) 15%
29-Nov-13 12 Critiques
TBD Final Exam Final 40%
Kate Gregory
Current car: Car
Objects are not Attributes
• On a class diagram, do not list attributes that are objects.
• Every person has a car, but not:
Person
Date of birth: Date
travel ()
Current car: Car
Kate Gregory
Associations
• A reference from one class to another is an association.
• Most relationships between classes are associations.– A person has a car.– An employee reports to a supervisor– An invoice is filled with items
Kate Gregory
Objects are not Attributes
Person
Date of birth: Date
travel ()
Car
Mileage: inthas
Kate Gregory
Guidelines For Identifying Associations
• Association often appears as a verb in a problem statement and represents relationships between classes. – A pilot flies a plane.– A manager approves a transaction
• Other phrases include part of, next to, works for, contained in, etc.
• Verbs help to qualify an association, links for passing messages
Kate Gregory
Common Association Patterns
• Location Association: next To, part of, contained in, ingredient of etc. – each employee is part of a department.
• Communication association: talk to, order to. – a customer places an order with an operator.
Kate Gregory
Binary Association Notation• On a class diagram, binary association is a line
connecting two classes• Both ends may be connected to the same class
Company PersonworksFor
employer employee
Person
marriedTo
Kate Gregory
Links to pass Messages
Listing
Description
MLS
Agent
Phone No
writeInfo() writeInfo()
Kate Gregory
Multiplicity• Multiplicity specifies the number of instances of associated
classes for each instance of this class. • It is given for roles within associations, parts within
compositions, repetitions, and other purposes. • A number or range of numbers is written on each end of the
line– lower bound .. upper bound. (0..1, 2..5 etc)– Single number (0, 1, etc)– * to indicate any number including 0– 1..* to indicate 1 and up
Kate Gregory
Single Multiplicity
• Exactly one province for each city.
Kate Gregory
Unlimited Multiplicity
• Sometimes there is no limit on the number of objects in the association.
• Any number of cities in a province
Kate Gregory
Fixed Multiplicity
• Sometimes a object must be associated with a fixed number of other objects
• obvious or business rules state limited number (2 teams, 4 agents)
Car W heels4
Kate Gregory
Range of Multiplicity
• The most common range is 0 to 1: a single optional value
• The person either has an employer or does not: in this model no-one has more than one employer
Kate Gregory
Range of Multiplicity
• Another popular range is 1 or more, represented as 1..*
Kate Gregory
Range of Multiplicity
• Other than 0 to 1 and 1 or more, ranges are unusual. – A car has 2 to 5 doors… OK, but is “car door” likely to
be an object?– A person has 0 to 5 credit cards… why can’t someone
have 6?
• Push back when customers tell you ranges for relationships; make sure there’s a real business reason for the restriction and not some leftover from legacy software that couldn’t model reality
Kate Gregory
Qualified Associations
• Sometimes multiplicity alone can’t describe the restrictions on a relationship– How many bank accounts does a customer have
with each bank?– Can two of a customer’s accounts have the
same number?• As long as they’re with different banks
Kate Gregory
Qualifier
Bank
Person
account#
*
.
Kate Gregory
OR Association• An OR association indicates a situation in which
only one of several potential associations may be in effect at one time for any single object.
Person
Company
Car {or}
Kate Gregory
Some Attributes Are Hard To Place
• Consider Company and Person– Person works for company– Where does the salary attribute go?
• Company?– Everyone makes the same salary
• Person?– All my jobs pay the same
• Salary is an attribute of the relationship
Kate Gregory
Association Class• An association class holds properties of the
association
• An association class is shown as a class symbol attached by a dashed line to an association path.
WorksFor
Personemployer employee
Company
salary
Kate Gregory
Association Class• Association class is like a normal class with
attributes and methods
• Adds extra information to the link
House for Sale Agent
ContractExpirydate
commission
Kate Gregory
Eliminate Unnecessary Associations
• Derived associations can be defined in terms of other associations. They are redundant; avoid.
Kate Gregory
Eliminate Unnecessary Associations
• Implementation association. Defer implementation-specific associations to the design phase. – For example, if inventory is implemented by using an Array
class, don’t show that relationship. Only show business relationships as determined by your analysis
• Ternary associations. Ternary or n-ary association is an association among more than two classes. Avoid.– Replace with a new class
Kate Gregory
N-Ary Association• Association among more than two classes. • Difficult to understand
GradeBook
StudentCourse
gradeexamlab
Year
semester
*
*
*
Kate Gregory
Convert N-Ary Associations• Create a class to represent the relationship
GradeBook
StudentCourse
labgradeexam
Year
Session
*
*
* *
Kate Gregory
Association Class Replaces N-Ary
House for Sale AgentContractContractExpirydate
commision
Client
**
Kate Gregory
• Aggregation is a form of association representing a “part of” relationship. For example, players are part of a team.
• A hollow diamond is attached to the end of the path to indicate aggregation.
• “there is no single accepted definition of the differences between aggregation and association”, Fowler, UML Distilled, 1997
Aggregation
P la y e rc la ss
T e a m1 *c o n sis tO f
Kate Gregory
Composition• Composition is a form of aggregation with strong
ownership to represent a component of a complex object. • The “part” may belong to only one whole.
– Compare a picture pasted into a Word document (composition) to a picture on a web page (aggregation)
• Cascading Destruction – can the part exist without the whole ?– A customer has a credit card. – A customer has a sales rep. – A window on the screen has a title, a scroll bar, menus, frame and
so on.
Kate Gregory
Composition Notation
• The UML notation for composition is a solid diamond at the “container” end of a path.
Window
Title Scroll bar Menu Frame
0..21
1 *
Kate Gregory
Composition
House for Sale picture
Kate Gregory
Classes without Associations
• Sometimes a class is sitting all alone on the diagram• Check the CRC cards
– Collaborations generally can’t happen if objects can’t find each other– Those connections are associations– Sometimes connections are temporary, so don’t create an association– Think about the relationships between the real things these classes
represent• Look for words like “has”, “uses”, “owns”, “contains”• A class without associations does not always need to be
dropped– Another layer (e.g. user interface) may use it– Ask yourself if it’s really something in your system
Kate Gregory
Cohesion and Coupling• Only certain classes need to know about other classes
-- this is coupling. The lower the coupling (number of connecting lines between classes) the better.
• Certain information should reside in the same location (class) – this is cohesion and makes classes self-sufficient. The higher cohesion, the better
• More on these in our metrics section later. Start to think about whether the classes you are shown exhibit high or low cohesion, and are tightly or loosely coupled.
Kate Gregory 35
Remember what class diagrams are for
• Reference– Names of classes– Names of methods– Parameters to methods– Names of attributes
• Excellent summary of decisions
• Visual understanding of the system
Kate Gregory
Next Week
• Inheritance
• Lab 3 Due– Think about concurrency issues– Rate your group members
• Read ahead if you like
Recommended