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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
-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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
What is the difference: Payment Unauthorized Payment Authorized Payment Is-in Payment PaymentState * 1 Unauthorized State Authorized State Spring 2010 ACS-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
Aggregation and Composition Consider Invoices and their Invoice Lines Question: Is the association aggregation or composition? ? ? * Invoice InvoiceLine Spring 2010 ACS-3913 Ron McFadyen
Aggregation and Composition Consider Sales and their SalesLineItems Is the association an aggregation or a composition? ? ? * Sale SalesLineItem Spring 2010 ACS-3913 Ron McFadyen
Aggregation and Composition Consider the General Calendar and its Course Specifications. Is the association an aggregation or a composition? Spring 2010 ACS-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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.
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
<<interface>> From Head First … <<interface>> Observer update() These classes implement the update operation StatisticsDisplay ForecastDisplay Spring 2010 ACS-3913 Ron McFadyen
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-3913 Ron McFadyen
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.4.1-9.4.4, 9.5 pages 178-182 Message types Synchronous Asynchronous Creation Reply Spring 2010 ACS-3913 Ron McFadyen
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-3913 Ron McFadyen
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-3913 Ron McFadyen
Message to ‘self’ - reflexive message : sale getTotal() calcTotal() Spring 2010 ACS-3913 Ron McFadyen
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-3913 Ron McFadyen
<<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-3913 Ron McFadyen
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 17-21. Behaviour in Singleton Pattern Spring 2010 ACS-3913 Ron McFadyen
Discuss construction of a sequence diagram Examples 9.1-9.8 pages 179-182 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-3913 Ron McFadyen