CS 247: Software Engineering Principles UML Modelling Agenda: UML class diagrams UML object diagrams UML sequence diagrams Reading: Martin Fowler, UML Distilled, 3rd ed, Addison-Wesley Professional, 2004. (Electronic text available from UW Library Web site) www.uml.org U Waterloo CS247 (Spring 2017) p.1/18
Software Modelling Publication[p] borrow(m,p) / BorrowItem(m, p, today) InStacks OnLoan return(p) / ReturnItem(p, today) lost A software model is an abstraction of a software system's code (already developed or to-be-developed). Analogous to construction plans U Waterloo CS247 (Spring 2017) p.2/18
Unified Modeling Language (UML) UML - A collection of notations for representing different views of a software design. Structural Diagrams Class diagram Component diagram Composite structure diagram Deployment diagram Object diagram Package diagram Behaviour Diagrams Activity diagram State diagram Use case diagram Interaction Diagrams Communication diagram Interaction overview diagram Sequence diagram Timing diagram U Waterloo CS247 (Spring 2017) p.3/18
UML Class Diagram Notation A box represents a class and defines: class name set of attributes (data fields, types), initial values set of operations (routines, signatures) Customer name: string address: string Customer( name ) name() : string address() : string addressis( newaddr : string ) : void U Waterloo CS247 (Spring 2017) p.4/18
Abstraction in Classes Classes can be expressed at different levels of abstraction. Customer Customer name address birthdate Customer name: string [1] {readonly} address: string + Customer( name:string ); + name() : string {query} + address() : string {query} + addressis( newaddr : string ) : void KEY: + public private # protected static pure virtual U Waterloo CS247 (Spring 2017) p.5/18
Associations An association between two classes indicates that there exists a physical or conceptual link between objects of those classes. Date day month year start date end date Rental Agreement rents driver customer Person firstname : string lastname : string Association name Vehicle manuf model color VIN rented car Role names U Waterloo CS247 (Spring 2017) p.6/18
Multiplicities Multiplicity annotations constrain the number of allowable links in an association. 0 m n 0 p X m..n R p..* Y For each object x of class X, there must be at least p links of association R linking x to object of class Y; and For each object y of class Y, there must be between m and n links of association R linking y to object of class X. No annotation means that the multiplicity is unspecified. U Waterloo CS247 (Spring 2017) p.7/18
Implementing Associations A B A B A B A B 1 0..1 A B 1 5 * * U Waterloo CS247 (Spring 2017) p.8/18
Association Class A class association represents link attributes - properties of the link, because they cannot be attributed to either of the end objects Person contestant * * Race Performance time : Time place : int this is an association class, models data and relationships that are associated with a pair of objects U Waterloo CS247 (Spring 2017) p.9/18
Aggregation 0..1 ProjectTeam * subteam * 1..* Developer Aggregation is a part-of relation between an aggregate (collection) and its members. part can be a member of more than one aggregate e.g., students can be members of more than one class roster part has an identity outside of the aggregate U Waterloo CS247 (Spring 2017) p.10/18
Composition PurchaseOrder total : Money delivered : boolean * 1..* purchased Product type : string colour : Colour 1 catalogued Catalog year : Date version : integer 1 * 1 CatalogEntry number : string Composition is a stronger part of relation between a composite object and its components: a part does not exist without its composite a part belongs to at most one composite the composite is responsible for creating, destroying members U Waterloo CS247 (Spring 2017) p.11/18
Another Example On an ipod, songs (and other media) are physically stored in a Library, and Playlists are virtual collections of songs. Library 1 * Song * {ordered} * Playlist U Waterloo CS247 (Spring 2017) p.12/18
Generalization The UML uses the term generalization for the subtype relationship between a base class and its derived classes. Every member of a derived class is a member of its base class. Attributes and associations of the base class are attributes and associations of the derived class. Transcript - GPA : float + calcgpa() 1 1 Student - name : string - ID : int Math Transcript + calcgpa() Engineering Transcript + calcgpa() U Waterloo CS247 (Spring 2017) p.13/18
World Cup The first round, or group stage, is a round-robin competition between thirty-two teams divided into eight groups. The two best teams of each group will progress to the knockout stage. The ranking of teams in each group will be based on: 1. Points in all group matches 2. Goal difference in all group matches 3. Goals scored in all group matches 4. Points in matches between tied teams 5. Goal difference in matches between tied teams 6. Goals scored in matches between tied teams 7. Drawing of lots In the knockout stage there will be four rounds, each eliminating the losers. The four rounds are: the round of 16, quarter-finals, semi-finals, and the final (plus a play-off for third place). A draw in the knockout stages will be followed by two 15-minute periods of extra time to determine a winner. If the teams are still tied, a penalty shootout will be held. Wikipedia U Waterloo CS247 (Spring 2017) p.14/18
UML Object Models An object model is a run-time instance of a class model. Every object is an instantiation of a specific class. Every link is an instantiation of a specific association. (Optional) object name class name object and class names are underlined attribute names (Optional)attribute values s1:student firstname = "John" lastname = "Rincewind" s2:student firstname = "Stuart" lastname = "McLean" takes takes c1:course prefix = "CS" number = 247 section = 001 term = Spring year = 2017 :Student firstname = "Abby" lastname = "Goodrum" takes :Course prefix = SE number = 464 section = 001 term = Spring year = 2017 U Waterloo CS247 (Spring 2017) p.15/18
UML Sequence Diagram Notation A UML Sequence Diagram is a graphical model of communication events between objects, as exhibited in one execution trace. event, method call entities, objects, systems, subsystems, actors Time :PurchaseOrder :Product :Product :Product printreceipt() print() print() print() return message lifeline of object execution occurrence U Waterloo CS247 (Spring 2017) p.16/18
Some UML Drawing Tools Can use any UML modelling or drawing tool that you would like. - Must be able to output PDF files. Visio OmniGraffle (Mac only) UMLet (open source, Windows / OS X / Linux) http://www.umlet.com/ U Waterloo CS247 (Spring 2017) p.17/18
Take Aways Recognition Recognize class diagrams: class, attribute, association, association name, rolename, association class. Recognize rules for designating data as objects or attributes. Comprehension Distinguish between aggregation and composition. Understand program behaviour described as a sequence diagram. Application Model a program's set of classes as a class diagram. Use multiplicities to constrain allowable instances of a class diagram. U Waterloo CS247 (Spring 2017) p.18/18