35
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence S.W. Lau and Y.K. Leung McGraw-Hill Education (Asia), 2005 Dr. Hussein Al-Zoubi

Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

Embed Size (px)

Citation preview

Page 1: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

Chapter 5Implementing UML

Specification (Part II)

Object-Oriented TechnologyFrom Diagram to Code with Visual Paradigm for

UML

Curtis H.K. Tsang, Clarence S.W. Lau and Y.K. Leung

McGraw-Hill Education (Asia), 2005Dr. Hussein Al-Zoubi

Page 2: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

2

Activity Diagrams An activity diagram is used to represent a

sequence of actions performed in an activity or a procedure.

It can also be used to model an algorithm, the computation flow of a control object or a subsystem.

In general, there are two approaches for implementing an activity diagram:

Implementing the control flow using the location within a program to hold the state of an object. Control statements, such as if-then-else statement and while statement, are used to implement the necessary branching and looping of the control flow of the activity diagram.

Implementing the control flow as a state machine.

Page 3: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

3

Activity Diagrams (cont’d) The following are the general rules for

translating the elements of an activity diagram into program code: Action state. It is translated to statements of

actions, such as method calls, computational statements.

Conditional branch. It is translated to an if-then-else statement.

Concurrent branch. It is translated to threads for each additional control flow.

Loop. A loop in the activity diagram is translated to a while-loop statement.

Page 4: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

4

Activity Diagrams (cont’d)

Page 5: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

5

Example – Control Object of the Simplified Vending Machine

while (true) { amount = 0.0; while (amount < price) { wait for a coin; add coin value to amount; } show all available soft drink; while (selection is not done) { wait for selection from user; if selection is “eject coins” { dispense coins; set selection to “done”; } else if selection is a valid soft drink { dispense change & soft drink concurrently; set selection to “done” } }}

Page 6: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

6

State Diagrams A state diagram is typically used to

model the dynamic behavior of a subsystem, a control object or even an entity object. Like activity diagrams, there are two approaches to implement a state diagram: Using the location within a program to hold

the state (for implementing active object or entity).

Using an explicit attribute to hold the state (for implementing inactive object or entity).

Page 7: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

7

State Diagrams (cont’d) The second approach is suitable for

implementing the state diagram of an inactive entity. We can implement the state diagram by applying the techniques below: Map the state diagram on to a class. Add a state attribute for storing the state

information. Map an event to a method and embed all

required state transitions and actions of the event in the method.

Page 8: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

8

State Diagrams (cont’d)public void event_n(….) { switch (state) { case state_k: if (guard_condition_w) { state = state_m; perform actions of the transition; } break; case state_v: … … }}

Page 9: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

9

State Diagrams (cont’d) For a composite state with sequential

substates, a nested (inner) class for implementing the

sequential substates. The parent state machine can then invoke

the method of the nested class for handling transitions within the nested state diagram.

Another way to implement the composite state is to transform the parent state diagram to eliminate the composite state so that it becomes a flat level state diagram.

Page 10: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

10

State Diagrams (cont’d) Another way to implement the composite

state is to transform the parent state diagram to eliminate the composite state

For a composite state with concurrent substates, create a nested class for implementing each

substate. The composite state is exited when all the

concurrent substates reach their final states.

Page 11: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

11

Example – Control Object of Vending Machine

Page 12: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

12

Example – Control Object of Vending Machine (cont’d)

class VendingMachineControl{ int _state; float _amount, _price; static final int WaitingCoin = 1; static final int WaitingSelection = 2; static final int DispensingSoftDrink = 3; static final int DispensingChange = 4; static final int EjectingCoins = 5;

Page 13: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

13

Example – Control Object of Vending Machine (cont’d)

public VendingMachineControl(float price) { _amount = 0; _state = WaitingCoin; _price = price; }

Page 14: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

14

Example – Control Object of Vending Machine (cont’d)

public void insertedCoin(float coinValue) { if (state == WaitingCoin) { amount += coinValue; if (amount >= price) { // fire transition state = WaitingSelection; show available soft drinks; } } } // insertedCoin

Page 15: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

15

Example – Implementing a State Diagram with Sequential Substates

Page 16: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

16

Example – Implementing a State Diagram with Sequential Substates (cont’d)

class dispenseControl { int _state; static final int DispensingSoftDrink = 1; static final int DispensingChange = 2; static final int Complete = 3; public dispenseControl() { _state = DispensingSoftDrink; }

Page 17: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

17

Example – Implementing a State Diagram with Sequential Substates (cont’d)

public boolean dispensedSoftDrink() { if (_state == DispensingSoftDrink) { _state = DispensingChange; dispense change; } return false; }

Page 18: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

18

Example – Implementing a State Diagram with Sequential Substates (cont’d)

public boolean dispensedChange() { if (_state == DispensingChange) { _state = Complete; return true; } return false; }

Page 19: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

19

Example – Implementing a State Diagram with Sequential Substates (cont’d)

class VendingMachineControl{..declaration of state attribute, constants, other attributes; declaration of inner class dispenseControl;..public VendingMachineControl(float price) { _amount = 0; _state = WaitingCoin; _price = price; _substate = new DispenseControl(); }

Page 20: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

20

Example – Implementing a State Diagram with Sequential Substates (cont’d)

public void dispensedSoftDrink() // VendingMachineControl { if (_state == Dispensing) { boolean isComplete = _substate.dispensedSoftDrink(); } }

Page 21: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

21

Example – Implementing a State Diagram with Sequential Substates (cont’d)

// VendingMachineControlpublic boolean dispensedChange() { if (_state == Dispensing) { boolean isComplete = _substate.dispensedChange(); if (isComplete) { amount = 0; _state = WaitingCoin; } } }

Page 22: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

22

Sequence Diagrams In a sequence diagram, the

collaborating objects communicate with each other through messages.

A message can be an invocation of a method or an actual message sent from the originating object to the target object.

When an object receives a message, it may in turn send out one or more messages to other objects.

Page 23: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

23

Sequence Diagrams (cont’d) We can translate a sequence diagram into

code by using the following techniques: Translate a message to an appropriate method call.

For example, a creation message is translated to a call to the constructor of the target object’s class,

Implement methods for handling incoming messages in the target object’s class

Map conditional branchings to conditional statements, such as if-else statement.

Implement active objects using threads. Map concurrent branching using concurrent threads.

Page 24: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

24

Sequence Diagrams (cont’d)

ClassB

{ ClassC o3; ClassD o4; method1(…) { o3.method2(..); o4.method3(..); }}

Page 25: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

25

Page 26: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

26

Page 27: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

27

Page 28: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

28

Page 29: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

29

Page 30: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

30

Page 31: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

31

Page 32: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

32

Page 33: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

33

Page 34: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

34

Page 35: Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

35