460 likes | 568 Views
Chapter 15. Interaction Diagrams. Most Common. Sequence Diagram Communication Diagram Sequence Diagrams illustrate interactions between classes of a program using class methods as message names,. Sample Code:. public class A { private B myB = null; public A () {
E N D
Chapter 15 Interaction Diagrams
Most Common • Sequence Diagram • Communication Diagram • Sequence Diagrams illustrate interactions between classes of a program using class methods as message names,
Sample Code: public class A { private B myB = null; public A () { myB = new B(); } public void doOne() { myB.doTwo(); myB.doThree(); } }
Fig. 15.1 duration of doOne() activity
Fig. 15.2 Communication Diagram sequence numbers Same interaction in a “network” diagram description
Strengths and Weaknesses • UML has put more thought into SDs • Better tool support for SDs • Better at showing “sequence” • CDs are more space efficient, boxes can go anywhere • Easier to modify a CD • CDs are “vertical” so fit on narrow pages of a book
Fig. 15.3 lifeline NOTE: If you need to extend you’ve run out of space
Fig. 15.4 NOTE: If you need to extend you can come back to the left.
Static vs Dynamic View • The Class Diagram is a static view of the program while the Sequence Diagram is a dynamic view. Both are useful.
Common Notation Fig. 15.5
Singleton Pattern • http://en.wikipedia.org/wiki/Singleton_pattern public class Singleton { // Private constructor suppresses // generation of a (public) default constructor private Singleton() {} private static class SingletonHolder { private static Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } }
Messages Fig. 15.7 also activation bar asynchronous with dashed lines
Message Return Values: • Two ways to show the return result • Using the message syntax returnVar = message(parameter). • Using a reply (or return) message line at the end of an activation bar
Fig. 15.9 representing a message sent to the same object; not just to an object of the same class this.clear()
Fig. 15.10 New Object Creation dashed line because UML says so
Fig. 15.11 I’m not sure where this fits in the Java paradigm?
Fig. 15.12 [more items] I think this notation is starting to get out of hand. I, because I am drawing on the whiteboard, would draw and arrow like I have. In general, these are called diagram frames
Fig. 15.13 I have no easy alternative to this; except to not bother trying to describe an entire algorithm.
Diagram Frames: Frame Operator Meaning
Fig. 15.14 I like this better
Sample Code: public class Sale { private List<SalesLineItem> lineItems = new ArrayList<SalesLineItem>(); public Money getTotal() { Money total = new Money(); Money subtotal = null; for ( SalesLineItem lineItem : lineItems ) { subtotal = lineItem.getSubtotal(); total.add( subtotal ); } return total; } // … }
Fig. 15.17 An alternative for Figure 15.16; nothing is official.
Fig. 15.18 nested loops
Fig. 15.19 sd == sequence diagram simplifies the first diagram
Code Example: public class Foo { public void doX() { // static method call on class Calendar Locale[] locales = Calendar.getAvailableLocales(); // … } // … }
Fig. 15.21 abstract method call individual implementations
Fig. 15.22 asynchronous call Guideline This arrow difference is subtle. And when wall sketching UML, it is common to use a stick arrow to mean a synchronous call because it's easier to draw. Therefore, when reading a UML interaction diagram don't assume the shape of the arrow is correct!
Code Example public class ClockStarter { public void startClock() { Thread t = new Thread( new Clock() ); t.start(); // asynchronous call to the 'run' method on the Clock System.runFinalization(); // example follow-on message } // … } // objects should implement the Runnable interface // in Java to be used on new threads public class Clock implements Runnable { public void run() { while ( true ) // loop forever on own thread { // … } } // … }