a little UML
UML for the Java Associate
Oh my! Umm, hey, I‛m really not ready yet, OK?
UML is Cool.
Rumor has it there are some UML questions on Sun’s new
Java Associates exam. Sadly, the new Head First book on OO isn’t quite ready yet (be sure to check hfoobook.com toward the end of 2005!), but we’ve taken a few random pages (and parts of pages) from it and put them together here. So, if you don’t mind the lack of organization, you should ﬁnd everything you need to know about UML (for the exam) in the following pages. Oh, and did we mention that the author of this book wrote most of the UML questions for the exam? :-)
this is a new chapter
What’s with all the rectangles?
NOTE: The “facing page” is not included in this little UML booklet!
The four boxes full of text over there on the facing page are our ﬁrst examples of UML. UML stands for Uniﬁed Modeling Language, and it’s the most common notation used for creating diagrams that describe object-oriented systems. It’s beyond the scope of this book to teach you everything there is to know about UML. However, we’ll be sprinkling in a lot of the really critical stuff throughout the course of the book. If you’re going to be moving to Objectopia, you should deﬁnitely have a reasonable understanding of UML. In this, our ﬁrst look at UML, we’ll look at the notation used to represent classes (probably the single most commonly used aspect of UML) as well as the notation used to represent individual objects.
A class is drawn as a rectangle with three sections. The name of the class (in bold) goes in the first section. attribute types Book
title : String
It’s less common to draw object diagrams than it is to draw class diagrams. But they can be useful for describing specific examples or complex relationships between objects. class name object name hfJava : Book title = “HF Java” author = “K & B” pages = 619
The 2nd section contains the attributes and the last contains the operations.
author : String pages : int showPage(int) search(String)
attributes operations attribute names
current attribute VALUES
arguments Notice that we don’t bother listing the operations when we’re drawing a single object instance. All objects of a given type (class) have the same set of operations, so it would be redundant to list them here.
UML stuff from HFOO
UML is fun
Here’s another little UML 101 section taken from a Chapter 1 page of HFOO, plus a few extra notes (since Chapter 9 hasn’t been written yet)...
Book title : String author : String pages : int showPage(int) search(String) 1..* Page number : int content : String show() search(String)
Associations are drawn in UML by connecting two classes with a line. Depending on the type of relation, there may be a diamond at one end of the line. In this example, the diamond is filled in, which means we’re showing a special type of association called “composition.” Don’t worry, we’ll have much more to say about the different types of associations in Chapter 8! The UML 101 lesson on the right mentions Chapter 9. Well, the truth is, Chapter 9’s not written yet! But there are a couple of key things you need to understand if you’re taking the Java Associates exam...
Composition vs. Association
The little diamond indicates a “composition” relationship. The easiest way to think about a composition relationship is as a “part-of” relationship: a Page is a part of a Book. If you look at the Book/Page example on the right (which uses composition), the key point here is that if you get rid of a Book, all of its Pages go away too! The Book is composed of Pages.
Read this as “a Book is composed of one or more Pages.”
Another key thing to understand about UML relationship notation is navigation. If a line between two classes has an arrow at one end of it (not a triangle, just an arrow...