1. Trang chủ
  2. » Công Nghệ Thông Tin

Teach Yourself UML in 24 Hours 3rd phần 4 potx

51 322 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 51
Dung lượng 372,29 KB

Nội dung

130 Hour 8 In the state diagram, I haven’t dealt with windows that are opened by other windows— in other words, with substates nested within other substates. When a history state remembers substates at all levels of nesting (as the Windows Working state does), the history state is said to be deep. If it remembers only the highest nested substate, the history state is shallow. You represent a deep history by putting H* in the circle. New in UML 2.0 UML 2.0 has added some new state-relevant symbols called connection points. They represent points of entry into a state or exits out of a state. Here’s an example: Imagine a couple of the states of a book in a library. At first, it’s residing on a shelf. If a borrower has called in to reserve the book, a librarian retrieves the book and brings it into the state of “Being checked out.” If a borrower comes to the library, browses through the shelves, selects the book, and then decides to borrow it, it enters the Being-checked-out state, but in a different way. You can think of each way of getting to the Being-checked-out state as going through a separate entry point. One more thing to be aware of: Suppose the borrower has borrowed more than some allotted limit or has a number of unpaid fines. If that’s the case, the book abruptly exits—via an exit point—from the Being-checked-out state. Figure 8.9 shows how to model all this in UML. Each entry point is modeled as an empty circle. The exit point is an encircled X. The circles are on the border of the state icon. Input Registering User Input [Interval Over] [TimeOut] Watching System Clock Updating Display Working Visualizing User Input H Keystroke or mouse movement Awaiting User Input FIGURE 8.8 The history state, symbolized by the H in the small circle, shows that a composite state remembers its active substate when the object transitions out of that composite state. 11.067232640X.chap08.qxd 2/20/04 10:31 AM Page 130 Working with State Diagrams 131 Why Are State Diagrams Important? The UML state diagram provides a variety of symbols and encompasses a number of ideas, all to model the changes that just one object goes through. This type of diagram has the potential to get very complex very quickly. Is it really necessary? In fact, it is. It’s important to have state diagrams because they help analysts, designers, and developers understand the behavior of the objects in a system. A class diagram and the corresponding object diagram show only the static aspects of a system. They show hierarchies and associations, and they tell you what the behaviors are. They don’t show you the dynamic details of the behaviors. Developers, in particular, have to know how objects are supposed to behave because they have to implement these behaviors in software. It’s not enough to implement an object: Developers have to make that object do something. State diagrams ensure that they won’t have to guess about what the object is supposed to do. With a clear picture of object behavior, the likelihood increases that the development team will produce a system that meets requirements. Building the Big Picture Now you can add behavioral elements to your big picture of the UML. Figure 8.10 presents the picture with the state diagram included. Summary Objects in a system change their states in response to events and totime. The UML state diagram captures these state changes. A state diagram focuses on the state changes in just one object. A rounded rectangle represents a state, and a line with an arrowhead represents a transition from one state to another. The state icon shows the name of the state and can hold activities as well. A tran- sition can occur in response to a trigger event and can entail an action. A transi- tion can also occur because of an activity in a state: A transition that takes place Residing on Shelf ended [reserved] Being Checked Out x FIGURE 8.9 Entry points and an exit point in a UML state diagram. 11.067232640X.chap08.qxd 2/20/04 10:31 AM Page 131 132 Hour 8 in this fashion is termed a triggerless transition. Finally, a transition can occur because a particular condition—a guard condition—holds true. Sometimes, a state consists of substates. Substates may either be sequential (occurring one after the other) or concurrent (occurring at the same time). A state that consists of substates is called a composite state. A history state indicates that a composite state remembers its substate when the object transitions out of that composite state. A history state may be either shallow or deep. These terms per- tain to nested substates. A shallow history remembers only the top-level substate. A deep history remembers all levels of substates. UML 2.0 provides symbols for modeling connection points—entry points into a state and exit points out of a state. State diagrams help analysts, designers, and developers understand the behavior of the objects in a system. Developers, in particular, have to know how objects are sup- posed to behave because they have to implement these behaviors in software. It’s not enough to implement an object: Developers have to make that object do something. Relationships Grouping Extension «Stereotype» {Constraint} Annotation Association Generalization Dependency Realization Package Note Behavioral Element State Use case Class Structural Elements Interface Actor FIGURE 8.10 The big picture of the UML now includes a behavioral element, the state diagram. 11.067232640X.chap08.qxd 2/20/04 10:31 AM Page 132 Working with State Diagrams 133 Q&A Q. What’s the best way to start creating a state diagram? A. It’s much like creating a class diagram or a use case model. In the class dia- gram, you list all the classes and then wrestle with the interclass associa- tions. In the state diagram, you first list the states of the object and then focus on the transitions. As you work through each transition, figure out whether a trigger event sets it off and whether any action takes place. Q. Must every state diagram have a final state (the one represented by the bull’s-eye)? A. No. An object that never turns off won’t have this state. Q. Any hints on laying out a state diagram? A. Try to arrange the states and transitions so that you minimize crossing lines. One objective of this diagram (and any other) is clarity. If people can’t understand the models you build, no one will use them, and your efforts— no matter how thorough and insightful—will be wasted. Workshop The quiz and exercises will transition you into the Learned State Diagrams state. As always, you’ll find the quiz answers in Appendix A, “Quiz Answers.” Quiz 1. In what important way does a state diagram differ from a class diagram, an object diagram, or a use case diagram? 2. Define these terms: transition, event, and action. 3. What is a triggerless transition? 4. What is the difference between sequential substates and concurrent sub- states? 11.067232640X.chap08.qxd 2/20/04 10:31 AM Page 133 134 Hour 8 Exercises 1. Suppose you’re designing a toaster. Create a state diagram that tracks the states of bread in the toaster. Include necessary triggering events, actions, and guard conditions. 2. Figure 8.7 shows the concurrent substates within the GUI’s Working state. Draw a diagram of the Screensaving state that includes concurrent sub- states. 3. Figure 8.9 shows two of the states of a library book. Using your general knowledge of libraries, expand the diagram to include the remaining states. Add appropriate substates and guard conditions. 11.067232640X.chap08.qxd 2/20/04 10:31 AM Page 134 HOUR 9 Working with Sequence Diagrams What You’ll Learn in This Hour: . What a sequence diagram is . How to apply a sequence diagram . How to model the creation of an object . How to work with some UML 2.0 additions to sequence diagrams . Where sequence diagrams fit in the big picture of the UML The state diagrams you learned about in the last hour zoom in on a single object. They show the changes an object goes through. The UML enables you to expand your field of view and show how an object interacts with other objects. In this expanded field of view, you’ll include an important dimension—time. The key idea here is that interactions among objects take place in a specified sequence, and the sequence takes time to go from beginning to end. When you create a system, you specify the sequence, and you use the UML sequence diagram to do it. What Is a Sequence Diagram? The sequence diagram consists of objects represented in the usual way (as named rectangles with the name underlined), messages represented as solid-line arrows, and time represented as a vertical progression. 12.067232640X.chap09.qxd 2/20/04 10:15 AM Page 135 136 Hour 9 Objects The objects are laid out near the top of the diagram from left to right. They’re arranged in any order that simplifies the diagram. Extending downward from each object is a dashed line called the object’s lifeline. Along the lifeline is a narrow rectangle called an activation. The activation represents an execution of an operation the object carries out. The length of the rectangle signifies the activation’s duration. Duration, and time in general, are represented in a rough, ordinal way. This means that each dash in a lifeline usu- ally doesn’t stand for a specific unit of time but is intended to give a general sense of duration. Figure 9.1 shows an object, lifeline, and activation. :Name FIGURE 9.1 Representing an object in a sequence diagram. Messages A message that goes from one object to another goes from one object’s lifeline to the other object’s lifeline. An object can also send a message to itself—that is, from its lifeline back to its own lifeline. UML represents a message as an arrow that starts at one lifeline and ends at another. The shape of the arrowhead shows what type of message it is. In UML 1.x, three arrowhead shapes were available. UML 2.0 has eliminated one of those shapes and, to my way of thinking, cut down on confusion. I’ll explain the messages, and then show you what UML 2.0 has eliminated. One type of message is a call. This is a request from the object sending the mes- sage to the object receiving the message. The request is for the receiver to carry out one of its (the receiver’s) operations. Usually, this entails the sender waiting for the receiver to carry out that operation. Because the sender waits for the receiver (that is, “synchs up” with the receiver), this message is also referred to as synchronous. UML signifies this message type with a filled arrowhead at the end of a solid line. It’s typically the case that a call involves a return message from the receiver, although modelers often omit the symbol for the return message. The symbol for the return message is an open-stick arrowhead with a dashed line. Figure 9.2 shows these symbols. 12.067232640X.chap09.qxd 2/20/04 10:15 AM Page 136 Working with Sequence Diagrams 137 Many Happy Returns A few words about that return message symbol. First, it might be a little confusing because it closely resembles a dependency arrow. Second, as you read more about UML, you might find differing representations for a return message. Documentation from the UML 1.x era sometimes shows this symbol with the open-stick arrowhead and sometimes with the same arrowhead as the call. UML 2.0 specifies the symbol shown in Figure 9.2, and that’s the one I’ll use. Another kind of message is asynchronous. With this one, the sender transfers control to the receiver and doesn’t wait for the operation to complete. The symbol for this message is an open-stick arrowhead, as Figure 9.3 shows. FIGURE 9.2 The UML symbol for a call and for a return. By the Way FIGURE 9.3 The UML symbol for an asynchronous message. The Missing Arrowhead What about that extra UML 1.x arrowhead? That was an open-stick half-arrowhead. (Imagine the arrowhead in Figure 9.3 with half the arrowhead missing.) UML 1.x used it to stand for asynchronous messages. The idea was to have one kind of sym- bol for an asynchronous message and another for a transfer-of-control message, but the boundaries among message categories sometimes became fuzzy. I’ll adopt the UML 2.0 categories and work only with the symbols in Figures 9.2 and 9.3. Time The diagram represents time in the vertical direction: Time starts at the top and progresses toward the bottom. A message that’s closer to the top occurs earlier in time than a message that’s closer to the bottom. By the Way 12.067232640X.chap09.qxd 2/20/04 10:15 AM Page 137 138 Hour 9 Thus, the sequence diagram is two-dimensional. The left-to-right dimension is the layout of the objects, and the top-to-bottom dimension shows the passage of time. Figure 9.4 shows the essential symbol set of the sequence diagram, with the sym- bols working together. The objects are laid out across the top. Each object’s lifeline is a dashed line extending downward from the object. A solid line with an arrow- head connects one lifeline to another and represents a message from one object to another. :Name1 :Name2 FIGURE 9.4 The symbols in a sequence diagram. In order to bring this important UML tool to life, we’ll apply it to some examples. As we do so, you’ll have the opportunity to work with some object-oriented con- cepts that form the basis for sequence diagrams. I’ll also be going back into classes, so it might seem that I’m digressing. I’m not. Trust me. Cars and Car Keys You might be familiar with the kind of car key that allows you to remotely lock and unlock a car. It also lets you open the car’s trunk. If you have one of these keys, you know what happens when you push the “lock” button. The car locks itself, and then it blinks its lights and beeps to let you know it’s finished locking its doors. A Class Diagram Let’s capture all this in a class diagram. Figure 9.5 shows the relationships among the CarOwner, Car, and CarKey classes, as well as some other concepts. The Car processes a message from the key and causes the appropriate behavior to take place. 12.067232640X.chap09.qxd 2/20/04 10:15 AM Page 138 [...]... of a sequence diagram—modeling the interactions in a domain defined by a class diagram The next example shows another context for applying sequence diagrams The Soda Machine Let’s move on to an example with a little more complexity You’ll recall that in Hour 6, “Introducing Use Cases” and Hour 7, “Working with Use Case Diagrams,” you read about the use cases of a soda machine Remember also that a use... type of combination To show a combination, frame the entire set of fragments, and use a dotted line as a border between adjoining interaction fragments The two types of combinations I think will be the most widely used are denoted by the alt operator and by the par operator In the alt combination, each fragment is an alternative and can proceed only under certain conditions Guard conditions indicate which... diagram for that one Finally, suppose the customer does not insert the correct change, and the soda machine is out of change? The sequence diagram for that scenario is in Figure 9.12 FIGURE 9.10 A sequence diagram that models the soldout scenario of the “Buy soda” use case 12.067232 640 X.chap09.qxd 144 FIGURE 9.11 A sequence diagram for the incorrect-change scenario 2/20/ 04 10:15 AM Page 144 Hour 9 :Customer... put just one scenario into a sequence diagram When you do this, you create an instance sequence diagram If you include all of a use case’s scenarios when you draw a sequence diagram, you create a generic sequence diagram Let’s put all our scenarios into one diagram 12.067232 640 X.chap09.qxd 2/20/ 04 10:15 AM Page 145 Working with Sequence Diagrams 145 We need some way of indicating conditions; one condition... aspect of modeling object–creation is the «create» keyword you put on the message sent from the creator object to the created object (Because the Register isn’t involved in this sequence, it doesn’t appear in the diagram.) 12.067232 640 X.chap09.qxd 148 FIGURE 9.15 A sequence diagram that models the best-case scenario of using a cell phone as an interface to a soda machine 2/20/ 04 10:15 AM Page 148 Hour 9... right-hand part of the figure shows that an object can instruct another object to destroy itself It does this by sending a message whose label is a «destroy» keyword 12.067232 640 X.chap09.qxd 2/20/ 04 10:15 AM Page 149 Working with Sequence Diagrams :Front :TransactionRecord :TransactionRecord X X «destroy» Framing a Sequence: Sequence Diagramming in UML 2.0 UML 2.0 adds a useful touch to sequence diagrams... shows how to reuse that interaction occurrence in the incorrect change scenario 149 FIGURE 9.16 An object can destroy itself (left), or it can receive an instruction to be destroyed (right) 12.067232 640 X.chap09.qxd 150 FIGURE 9.17 Framing a sequence diagram in UML 2.0 2/20/ 04 10:15 AM Page 150 Hour 9 sd BuySoda :Customer :Front :Register :Dispenser accept(cash, selection) getCustomerInput(cash, selection)... useful for modeling the scenarios of a use case In this example, you’ll model scenarios of the “Buy soda” use case You’ll begin with a class diagram, as you did in the preceding example The class diagram will model the entities that make up a soda machine To keep it simple, assume three components—a front, a register, and a dispenser Engineers who make a living designing and building soda machines, of course,... is to imagine the difference between a snapshot and a movie The object diagram is the snapshot: It shows how instances of classes are linked together in an instant of time (“Instants and instances” Remember?) The communication diagram is the movie: It shows interactions among those instances over time To represent a message, you draw an arrow near the link between two objects The arrow points to the... Figure 10.11 shows the communication diagram 13.067232 640 X.chap10.qxd 1 64 2/20/ 04 10:57 AM Hour 10 FIGURE 10.11 Modeling object creation in the best-case scenario in the cell-phone–enabled soda machine Page 1 64 1: getButtonPress(creditCardNumber) 3: getButtonPress(selection) :Customer :CellPhone 2: processCustomerInfo(creditCardNumber) 4: processCustomerInfo(selection) 2.1: displayPrompt(“Approved”) :Front . using a cell phone as an interface to a soda machine. By the Way 12.067232 640 X.chap09.qxd 2/20/ 04 10:15 AM Page 148 Working with Sequence Diagrams 149 Framing a Sequence: Sequence Diagramming in. sequence diagram for the incorrect-change scenario. 12.067232 640 X.chap09.qxd 2/20/ 04 10:15 AM Page 144 Working with Sequence Diagrams 145 We need some way of indicating conditions; one condition. exit point is an encircled X. The circles are on the border of the state icon. Input Registering User Input [Interval Over] [TimeOut] Watching System Clock Updating Display Working Visualizing User

Ngày đăng: 13/08/2014, 08:21

TỪ KHÓA LIÊN QUAN