Download presentation
Presentation is loading. Please wait.
Published byMalin Olofsson Modified over 5 years ago
1
Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development process. More detail added as time goes by. A static view of classes – shows structure: data, operations, and associations. Spring 2010 ACS Ron McFadyen
2
Represented by a rectangle with possibly 3 compartments
Classes Represented by a rectangle with possibly 3 compartments Customer Customer getName() checkCreditRating() Customer Customer Name Address Name Address getName() checkCreditRating() Spring 2010 ACS Ron McFadyen
3
Some classes are stereotyped:
«Singleton» dbFacade Some classes are stereotyped: Later in the course we study the Singleton design pattern. Stereotyping dbFacade as singleton conveys substantial information about the class and its behaviour. Some methodologies have 3 stereotypes for “analysis” classes: boundary, control, entity Spring 2010 ACS Ron McFadyen
4
Classes Abstract Classes
An abstract class is one that is never instantiated. To indicate an abstract class, the class name is given in italics or by using an “abstract” stereotype. Composite See last 2 examples Spring 2010 ACS Ron McFadyen
5
Students have names, addresses, etc;
Attributes an object contains data which are defined as part of the Class definition examples: Students have names, addresses, etc; Courses have titles, descriptions, prerequisite information. Student Rectangle name address corner: Point Level of detail present will depend on whether you are in analysis or design, and your purposes at the time Spring 2010 ACS Ron McFadyen
6
To what degree is an attribute visible to other classes? Private –
Attributes To what degree is an attribute visible to other classes? Private – Public + Protected # Package ~ Student -name -address Spring 2010 ACS Ron McFadyen
7
-payments[0..*]: Currency -name -address[1..3] {unique}
Attributes Default values = Derived values / Multiplicity [ ] Ordering {ordered} Uniqueness {unique} Invoice Student -date:Date = today -/total: Currency -payments[0..*]: Currency -name -address[1..3] {unique} Spring 2010 ACS Ron McFadyen
8
What are the responsibilities of a class? What can it do? Visibility
Operations. What are the responsibilities of a class? What can it do? Visibility Parameters Signature the name, parameters, and return type of the operation Student +getName() +getGPA(term :Term, gpaType: String) Spring 2010 ACS Ron McFadyen
9
correspond to verbs expressing a relationship between classes example
Associations correspond to verbs expressing a relationship between classes example a Library Member borrows a Copy of a Book Multiplicities we indicate via multiplicities the range of allowable cardinalities for participation in an association examples: 1, 1..*, 0..*, *, 1..3 Spring 2010 ACS Ron McFadyen
10
you can name the relationship and indicate how to read it
Associations Names and roles you can name the relationship and indicate how to read it you can give role names for participating objects The name of the relationship and the direction for reading the name 1..* Works for 1 Person Company employee employer The role of a Company in this relationship The role of a Person in this relationship Spring 2010 ACS Ron McFadyen
11
a Library Member borrows a Copy of a Book
Associations example: a Library Member borrows a Copy of a Book * borrows * Member Book borrower Spring 2010 ACS Ron McFadyen
12
An employee is supervised by an employee
Associations example: An employee is supervised by an employee reports to supervised * Employee supervisor 0,1 A reflexive association Spring 2010 ACS Ron McFadyen
13
Objects Class instances
An individual object (instance of a class) is shown with naming information underlined (usually) Joe: Customer A customer named Joe : Customer An unnamed customer Spring 2010 ACS Ron McFadyen
14
Consider an instance of a class
Objects Object state Consider an instance of a class Collectively, the specific attribute values and connections to other objects is known as the state of the object Analysis uncovers states of interest. These can be modeled using statechart diagrams … later Spring 2010 ACS Ron McFadyen
15
a generalization is a relationship between a general thing (the
superclass or parent class) and a more specific kind of thing (the subclass or child class) example: a StaffMember is a specialized kind of LibraryMember a StudentMember is a specialized kind of LibraryMember LibraryMember StaffMember StudentMember Spring 2010 ACS Ron McFadyen
16
Motivation for partitioning a class into subclasses:
subclass has additional attributes of interest subclass has additional associations of interest subclass is operated on, handled, reacted to, or manipulated differently than the superclass or other subclasses Spring 2010 ACS Ron McFadyen
17
Multiple subclasses can be grouped to indicate they are related
Generalization Multiple subclasses can be grouped to indicate they are related subclasses LibraryMember StaffMember StudentMember It then becomes meaningful to consider certain constraints: complete, incomplete, disjoint, overlapping Spring 2010 ACS Ron McFadyen
18
Inheritance of attributes and behaviour:
Generalization Inheritance of attributes and behaviour: everything a LibraryMember can do, a StaffMember can do everything a LibraryMember can do, a StudentMember can do If a LibraryMember can borrow a book, so can a StaffMember and a StudentMember a StaffMember and a StaffMember have all the attributes the LibraryMember has, and possibly more Specialization: there are some things that a specialized class can do that a LibraryMember cannot LibraryMember StaffMember StudentMember Spring 2010 ACS Ron McFadyen
19
Every payment, regardless of whether it is cash, credit, or cheque, has an Amount and it is associated with a Sale Pays-for Payment Sale 1 1 Amount: money Cash Payment Credit Payment Cheque Payment * 1 1 1 CreditCard Cheque Spring 2010 ACS Ron McFadyen
20
The name Payment is italicized - meaning it is an abstract class
Amount: money Cash Payment Credit Payment Cheque Payment The name Payment is italicized - meaning it is an abstract class An abstract class is a class that will never be instantiated; only its subclasses can exist If “Payment” was not in italics then a Payment could exist that is not a Cash, Credit, or Check payment Spring 2010 ACS Ron McFadyen
21
What is the difference:
Payment Unauthorized Payment Authorized Payment Is-in Payment PaymentState * 1 Unauthorized State Authorized State Spring 2010 ACS Ron McFadyen
22
Aggregation and Composition
both are associations used to denote that an object from one class is part of an object of another class An example of Aggregation: a course is part of an honours programme. The same module could be part of several honours courses * * HonoursProgramme Course Suppose the course “UML and Patterns” is part of both the “Software Engineering” and the “Computer Science” honours programmes Spring 2010 ACS Ron McFadyen
23
Aggregation and Composition
Composition is similar to, but stronger than aggregation. If you specify composition, then you are saying that one object owns its parts. Board Square * A Board is made up of several Squares. A Square will belong to just one Board. If a Board is deleted, then its Squares are deleted too. What is the multiplicity at the composition end of the association? Spring 2010 ACS Ron McFadyen
24
Aggregation and Composition
Consider Invoices and their Invoice Lines Question: Is the association aggregation or composition? ? ? * Invoice InvoiceLine Spring 2010 ACS Ron McFadyen
25
Aggregation and Composition
Consider Sales and their SalesLineItems Is the association an aggregation or a composition? ? ? * Sale SalesLineItem Spring 2010 ACS Ron McFadyen
26
Aggregation and Composition
Consider the General Calendar and its Course Specifications. Is the association an aggregation or a composition? Spring 2010 ACS Ron McFadyen
27
Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent groups, and another may represent an individual item, a leaf. We will examine the composite pattern later in the course. At this time, we are many concerned with its structural aspect. Consider the class diagram that follows. What objects does it allow us to instantiate and how will they relate to one another? Spring 2010 ACS Ron McFadyen
28
Composite Pattern Consider a UML class diagram for machines. Machines may be complex and contain other machines. * * 1 PlantFloor MachComponent getMachineCount() 1 Machine MachComposite components: List getMachineCount() getMachineCount() Spring 2010 ACS Ron McFadyen
29
An object diagram illustrating the machine on floor 5 of the plant
Composite Pattern An object diagram illustrating the machine on floor 5 of the plant floor5 :Plantfloor M1: MachComposite L1: Machine L2: Machine M9: MachComposite L7: Machine L4: Machine Spring 2010 ACS Ron McFadyen
30
Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object is the component, it is the decorated object. The decorator conforms to the interface of the enclosed component and so its presence is transparent to the components clients. The decorator forwards requests to the component, but may perform some processing before/after doing so. We will examine the decorator pattern later in the course. At this time, we are many concerned with its structural aspect. Consider the class diagram that follows. What objects does it allow us to instantiate and how will they relate to one another? Spring 2010 ACS Ron McFadyen
31
Decorator Pattern UML class diagram 1 1 1 sale DecoratedReceipt
print() What would a typical object diagram look like? receipt Decorator print() print() other() How does the Decorator pattern differ from the Composite pattern? timeOfDay productCoupon moneySaved Spring 2010 ACS Ron McFadyen
32
Bidirectional is default
Class Diagram Navigability This constraint specifies if a direction can be followed – whether one can navigate from one type of object to another type of object by following an association path. * Student X History -history[0..*] : History Bidirectional is default If an association can be navigated then support for that is required In above we say we can navigate from Student to History, but not the other way Spring 2010 ACS Ron McFadyen
33
Class Diagram Navigability
In the example below, student could be implemented with a history attribute: * Student X History -history[0..*] : History history[0..*] provides for the association to be navigated – a student will know its history, a student can send a message to a History object Spring 2010 ACS Ron McFadyen
34
Used when there is data or behaviour related to the association
Class Diagram Association classes Used when there is data or behaviour related to the association Useful for many to many associations * Student * Section A student registers for a section and a section has many students registered Spring 2010 ACS Ron McFadyen
35
Suppose we need to indicate a mark is kept to describe the association
Class Diagram Association classes Suppose we need to indicate a mark is kept to describe the association * * Student Section Class termMark examMark grade gpa() Spring 2010 ACS Ron McFadyen
36
Class Diagram Association classes
An association class is an association and it is a class– it can participate in associations Student * * Section Class 0,1 * Exam termMark examMark grade gpa() Spring 2010 ACS Ron McFadyen
37
Class Diagram Association classes
For a given student and section there can be only one occurrence of Class. This rule is quite strict or severe, but could be what we require here for our business rules. Student * * Section Class 0,1 * Exam termMark examMark grade gpa() Spring 2010 ACS Ron McFadyen
38
Consider the following where the *-* is for Student and Course
Class Diagram Association classes Consider the following where the *-* is for Student and Course Student * * Course Class With only one occurrence of class for a given student and course, we would only be keeping the most recent values for term, grade, etc. 0,1 * Exam term section grade gpa() Spring 2010 ACS Ron McFadyen
39
Class Diagram Association classes
To allow more flexibility, the modeler might promote his/her association class to a full class, as in: 1 1 Student Course * * Class 0,1 * Exam term section grade gpa() Spring 2010 ACS Ron McFadyen
40
An n-ary association is an association among 3 or more classes
Class Diagram N-ary associations An n-ary association is an association among 3 or more classes Section * * * Term Student enrollment Spring 2010 ACS Ron McFadyen
41
Class Diagram N-ary associations
Each instance of the association is an n-tuple of values from the respective classes. For each association we have one student, one section, one term The multiplicity on a role represents the potential number of instance tuples in the association when the other n-1 values are fixed. For a given student and term, we have many sections, … Section * * * Term Student enrollment Spring 2010 ACS Ron McFadyen
42
Class Diagram N-ary associations
Consider a team, goalies, and the season. We could record the performance of each goalie for each team and each season. Year season * * * Player Team goalie team Record goalsFor goalsAgainst Wins Losses ties Spring 2010 ACS Ron McFadyen
43
Comparing different models
Class Diagram Comparing different models We’ll examine the ternary association from last day, and two other models. Often ternary examples appear in texts without any real context. We will think now of the goalie statistics in a larger framework. To determine which model is best or better means we have to consider the business rules that are reflected in a model. What business rules do we need in a hypothetical system? Spring 2010 ACS Ron McFadyen
44
Class Diagram N-ary associations Model A
Consider a team, its goalies, and the season. We could record the performance of each goalie for each team and each season. Model A Year season * * * Player Team goalie team A ternary association capturing an interaction amongst 3 objects. This model may only be good for capturing goalie statistics by team, by season Record goalsFor goalsAgainst Wins Losses ties Spring 2010 ACS Ron McFadyen
45
Now, consider binary associations only
Class Diagram Now, consider binary associations only Consider some other business rules: BR: Each season teams register to participate in the sports league. Model B * * Year Team season team Note that we could use an association class for additional attributes to describe the association, or for the association participate in associations Note that this business rule wasn’t covered in the Model A Spring 2010 ACS Ron McFadyen
46
Using binary associations Consider business rules:
Class Diagram Using binary associations Consider business rules: BR: Each year each registered team has players and some of these will play as goalies. Note that a goalie could play on multiple teams each year. Model B * * Year registers Team season team RegTeam * playsAsGoalie * Player team goalie Now we are also capturing the fact that registered teams have goalies Spring 2010 ACS Ron McFadyen
47
Using binary associations Consider business rules:
Class Diagram Using binary associations Consider business rules: BR: Each registered team keeps statistics for each goalie Model B * * Year Team season team RegTeam * * Player team goalie Record goalsFor goalsAgainst Wins Losses ties Spring 2010 ACS Ron McFadyen
48
Using binary associations
Class Diagram Using binary associations It’s clear with this model that we are recording registered teams separately from the performance statistics of goalies. Model A does not facilitate team-related information. RegTeam is a place to keep information such as president, colours, homeField Model B * * Year Team season team RegTeam * * Player team goalie team Record goalsFor goalsAgainst Wins Losses ties Spring 2010 ACS Ron McFadyen
49
Model C, another alternative
Class Diagram Model C, another alternative One binary association between Year and Team, and a 3-ary (ternary) association amongst Year, Team, and Player RegTeam * * Year Team season team * team * season * Player goalie Record Which model is better, model B or model C? Does model C allow some connections that model B does not? goalsFor goalsAgainst Wins Losses ties Spring 2010 ACS Ron McFadyen
50
Object Diagram An object diagram is an object graph showing objects (possibly named and including attribute values) and links. A link shows a connection from one object to another.
51
e.g. a class diagram Consider a UML class diagram for machines. Machines may be complex and contain other machines. * * 1 PlantFloor MachComponent getMachineCount() 1 Machine MachComposite components: List getMachineCount() getMachineCount() Spring 2010 ACS Ron McFadyen
52
An object diagram illustrating the machine on floor 5 of the plant
e.g. One object diagram An object diagram illustrating the machine on floor 5 of the plant floor5 :Plantfloor M1: MachComposite links objects L1: Machine L2: Machine M9: MachComposite L7: Machine L4: Machine Spring 2010 ACS Ron McFadyen
53
Links, objects, object diagrams
Section 7.8.1 Goes through a sequence of diagrams illustrating how an analysts works out the solution to a modeling problem Analyst constructs object diagrams, then decides on the proper class diagram. Figures 7-27, 28, 29, 30 7-27 first draft – has a problem with the address objects 7-28 second draft – happens to use containment notation for composition – analyst clarifies role of addresses in the solution 7-29 above revised, but now correctly drawn 7.30 class diagram that fits the final object diagram Spring 2010 ACS Ron McFadyen
54
Links, objects, object diagrams
Section 7.8.1 Figure 7-30 CarSharer startsAt 1 1 1 registers * Journey Address 1 1 endsAt -homeAddress 1 Spring 2010 ACS Ron McFadyen
55
There is no behaviour defined, no method coded
Interfaces Section 7.5.5 An interface is special type of class that cannot be instantiated. An application can never instantiate an interface. An interface defines a set of public attributes and operations that some class must use (depends on) There is no behaviour defined, no method coded Some other class inherits the interface and provides the implementation Spring 2010 ACS Ron McFadyen
56
From Head First … package headfirst.observer.weatherobservable;
import java.util.Observable; import java.util.Observer; public class ForecastDisplay implements Observer, … public void update(Observable observable, Object arg) { if (observable instanceof WeatherData) { WeatherData weatherData = (WeatherData)observable; lastPressure = currentPressure; currentPressure = weatherData.getPressure(); display(); } public void display() { System.out.print("Forecast: "); if (currentPressure > lastPressure) { System.out.println("Improving weather on the way!"); } … Spring 2010 ACS Ron McFadyen
57
From Head First … package headfirst.observer.weatherobservable;
import java.util.Observable; import java.util.Observer; public class StatisticsDisplay implements Observer, DisplayElement { private float maxTemp = 0.0f; … public void update(Observable observable, Object arg) { if (observable instanceof WeatherData) { WeatherData weatherData = (WeatherData)observable; float temp = weatherData.getTemperature(); display(); } public void display() { System.out.println("Avg/Max/Min temperature = " … Spring 2010 ACS Ron McFadyen
58
<<interface>>
From Head First … <<interface>> Observer update() These classes implement the update operation StatisticsDisplay ForecastDisplay Spring 2010 ACS Ron McFadyen
59
Objects are represented horizontally across the top of the diagram
Sequence Diagram Objects are represented horizontally across the top of the diagram Each object has a lifeline some exist before and/or after some are created during some are destroyed during An active object is indicated by a narrow rectangle Focus of control Time is represented vertically down the diagram. Time moves forward as you go downwards Iteration is shown with a frame Spring 2010 ACS Ron McFadyen
60
Reading: See sections 9.1, 9.2, 9.3, 9.4.1-9.4.4, 9.5 pages 178-182
Sequence Diagram Reading: See sections 9.1, 9.2, 9.3, , 9.5 pages Message types Synchronous Asynchronous Creation Reply Spring 2010 ACS Ron McFadyen
61
Diagramming notation :sale :sale s4:sale s4:sale
An unnamed sale object A sale object named s4 s4 s4 Sale An object named s4 The class Sale Spring 2010 ACS Ron McFadyen
62
Sequence Diagram named Make payment
sd Make payment : register : sale makePayment() makePayment() payment() : payment Objects that pre-exist the collaboration are shown at top of diagram. Other objects are shown where/when they are created Spring 2010 ACS Ron McFadyen
63
Message to ‘self’ - reflexive message : sale getTotal() calcTotal()
Spring 2010 ACS Ron McFadyen
64
Iteration :Sale :SalesLineItem t=getTotal() st = getSubTotal()
loop [I < numItems] st = getSubTotal() Iteration loop is one of the keywords you can use to specify the nature of the fragment Note page 167 and use of alt Spring 2010 ACS Ron McFadyen
65
<<Singleton>>
e.g. Singleton pattern Suppose CarMatchOffice works according to the Singleton Pattern. This means there must be at any time, at most, one instance of CarMatchOffice. The getInstance operation returns the single instance of the class. getInstance will create the instance if it does not already exist. <<Singleton>> CarMatchOffice instance getInstance() Spring 2010 ACS Ron McFadyen
66
Figure 17-21. Behaviour in Singleton Pattern
When you ask for the instance of CarMatchOffice, there are two ways it can complete. client CarMatchOffice alt [inst == null] getInstance() CarMatchOffice() :CarMatchOffice Inst=CarMatchOffice() [else] getInstance() getAddress() Figure Behaviour in Singleton Pattern Spring 2010 ACS Ron McFadyen
67
Discuss construction of a sequence diagram
Examples pages Discuss construction of a sequence diagram Problem: create a sequence diagram to model the messages that occur when a Journey is created Figure 9-43 shows a diagram with CarSharer, Address, Journey, … Example 9-8 shows a scenario where the addresses are geolocated. Note that geolocate() is a method that manages this. Spring 2010 ACS Ron McFadyen
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.