UML a crash course Alex Lo Brian Kiefer
UML a crash course Alex Lo Brian Kiefer
Overview Classes Class Relationships Interfaces Objects States Worksheet
Classes Drawn as a three section box: n n n Name Fields Functions
Classes Fields and functions given visibility signs + sign indicates public - sign indicates private # sign indicates protected Fields are shown as name: type Functions are shown as Function(argument 1: type, …): return. Type Italic class or method names indicate the class is abstract Static fields and functions are underlined
Cooper Example public abstract class Person { protected String person. Name; private int age; public Person (String name) { person. Name = name; } static public String make. Job () {return "hired"; } public int get. Age () {return age; } private void split. Names () { } abstract String get. Job (); }
Alternate Diagrams
Packages are depicted as large folders with classes contained inside Package Name
Notes UML does not require that you show everything To draw a note in UML
Class Relationships Use a solid line with a hollow arrowhead to show a parent class Class 2 extends Class 1
Cooper Example public class Employee extends Person { public Employee (String name) { super (name); } public String get. Job () { return "Research Staff"; }}
Relationships A single line often represents that one class contains an instance of another Use a diamond to show that one class has a collection of the other class Sometimes a small circle is used to show that the instance is singular
Relationship Cardinality Exactly 1 Many (0 and up) Optional (0 or 1) One or more N to M (not shown)
Relationships An open arrow indicates navigability – it shows that one class has a ref to another and the other may not
Dependency A dashed arrow shows a dependency on a class that does not use an object ref n Example: using static method of another class others?
Other Relations Another relation can be done using the dashed arrows like throwing (bind can be any keyword, such as throw, send, become, friend, etc)
Interfaces are drawn similarly to classes, except without a field area (usually) and the <<interface>> stereotype An interface which you don’t need to define can be drawn as a “lollipop”
Interfaces To show a class implements an interface, use a dashed line with an open arrowhead
Notes on Interfaces While interfaces and their methods are always abstract in java, their methods do not appear italicized. As opposed to abstract classes and abstract methods. *This was disagreed on between Cooper and our book.
Objects You can show an object by giving its name and type separated by a colon (or just either/or) and underlining it A line between objects indicates that one object has a reference to another.
Calling Methods from Objects You can show a sequence of objects calling methods of other objects n n n The order is from top to bottom The dashed line indicates the object over time A bold outline of the box represents the class is running on a separate thread
States UML can also be used to model the various states of a program or method n n Think Introduction to Logic Design Think events (clicks, key presses, etc. )
Other uses of UML Collaboration diagrams Use case diagrams Activity diagrams Deployment diagrams Etc.
More Resources Our book, p 442 -447 Safari Online: Java™ Design Patterns: A Tutorial By James W. Cooper Chapter 2 UML Distilled
Discussion
- Slides: 25