SlideShare a Scribd company logo
1 of 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 S.W. Lau and Y.K.
Leung
McGraw-Hill Education (Asia), 2005
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.
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.
4
Activity Diagrams (cont’d)
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”
}
}
}
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).
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.
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:
…
…
}
}

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
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.
11
Example – Control Object of
Vending Machine
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;
13
Example – Control Object of Vending
Machine (cont’d)
public VendingMachineControl(float
price)
{
_amount = 0;
_state = WaitingCoin;
_price = price;
}
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
15
Example – Implementing a State Diagram
with Sequential Substates
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;
}
17
Example – Implementing a State Diagram
with Sequential Substates (cont’d)
public boolean dispensedSoftDrink()
{
if (_state == DispensingSoftDrink) {
_state = DispensingChange;
dispense change;
}
return false;
}
18
Example – Implementing a State Diagram
with Sequential Substates (cont’d)
public boolean dispensedChange()
{
if (_state == DispensingChange) {
_state = Complete;
return true;
}
return false;
}
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();
}
20
Example – Implementing a State Diagram
with Sequential Substates (cont’d)
public void dispensedSoftDrink() // VendingMachineControl
{
if (_state == Dispensing) {
boolean isComplete = _substate.dispensedSoftDrink();
}
}
21
Example – Implementing a State Diagram
with Sequential Substates (cont’d)
// VendingMachineControl
public boolean dispensedChange()
{
if (_state == Dispensing) {
boolean isComplete =
_substate.dispensedChange();
if (isComplete) {
amount = 0;
_state = WaitingCoin;
}
}
}
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.
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.
24
Sequence Diagrams (cont’d)
ClassB
{
ClassC o3;
ClassD o4;
method1(…)
{
o3.method2(..);
o4.method3(..);
}
}

More Related Content

Similar to Jar chapter 5_part_ii

This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
indiaartz
 
Unity3D Scripting: State Machine
Unity3D Scripting: State MachineUnity3D Scripting: State Machine
Unity3D Scripting: State Machine
Sperasoft
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org
AbbyWhyte974
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org
MartineMccracken314
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella
Obeo
 
Simple array Java code.The “Park-a-lot” parking garage currently o.pdf
Simple array Java code.The “Park-a-lot” parking garage currently o.pdfSimple array Java code.The “Park-a-lot” parking garage currently o.pdf
Simple array Java code.The “Park-a-lot” parking garage currently o.pdf
fasttracktreding
 

Similar to Jar chapter 5_part_ii (20)

03 fsm how_toimplementai_state_20161006_jintaeks
03 fsm how_toimplementai_state_20161006_jintaeks03 fsm how_toimplementai_state_20161006_jintaeks
03 fsm how_toimplementai_state_20161006_jintaeks
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Building Testable Reactive Apps with MVI
Building Testable Reactive Apps with MVIBuilding Testable Reactive Apps with MVI
Building Testable Reactive Apps with MVI
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonf
 
Unity3D Scripting: State Machine
Unity3D Scripting: State MachineUnity3D Scripting: State Machine
Unity3D Scripting: State Machine
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Flutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdfFlutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdf
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
 
Jetpack Compose untuk UI Masa Depan Bagian 2 - Sidiq Permana
Jetpack Compose untuk UI Masa Depan Bagian 2 - Sidiq PermanaJetpack Compose untuk UI Masa Depan Bagian 2 - Sidiq Permana
Jetpack Compose untuk UI Masa Depan Bagian 2 - Sidiq Permana
 
Architectures in the compose world
Architectures in the compose worldArchitectures in the compose world
Architectures in the compose world
 
Model Execution and System Simulation
Model Execution and System SimulationModel Execution and System Simulation
Model Execution and System Simulation
 
[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법
 
[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture
 
From Declarative to Imperative Operation Specifications (ER 2007)
From Declarative to Imperative Operation Specifications (ER 2007)From Declarative to Imperative Operation Specifications (ER 2007)
From Declarative to Imperative Operation Specifications (ER 2007)
 
Simple array Java code.The “Park-a-lot” parking garage currently o.pdf
Simple array Java code.The “Park-a-lot” parking garage currently o.pdfSimple array Java code.The “Park-a-lot” parking garage currently o.pdf
Simple array Java code.The “Park-a-lot” parking garage currently o.pdf
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 

More from Reham Maher El-Safarini

More from Reham Maher El-Safarini (20)

Ux
Ux Ux
Ux
 
Global threat-landscape report by fortinet.
Global threat-landscape report by fortinet.Global threat-landscape report by fortinet.
Global threat-landscape report by fortinet.
 
Dynamics AX/ X++
Dynamics AX/ X++Dynamics AX/ X++
Dynamics AX/ X++
 
Microsoft sql-and-the-gdpr
Microsoft sql-and-the-gdprMicrosoft sql-and-the-gdpr
Microsoft sql-and-the-gdpr
 
AWS Cloud economics
AWS Cloud economicsAWS Cloud economics
AWS Cloud economics
 
Cloud skills development
Cloud skills developmentCloud skills development
Cloud skills development
 
AWS cloud adoption framework (caf)
AWS cloud adoption framework (caf)AWS cloud adoption framework (caf)
AWS cloud adoption framework (caf)
 
Application and database migration workshop
Application and database migration workshopApplication and database migration workshop
Application and database migration workshop
 
Containers on AWS
Containers on AWSContainers on AWS
Containers on AWS
 
Security and governance with aws control tower and aws organizations
Security and governance with aws control tower and aws organizationsSecurity and governance with aws control tower and aws organizations
Security and governance with aws control tower and aws organizations
 
Digital transformation on aws
Digital transformation on awsDigital transformation on aws
Digital transformation on aws
 
Security in the cloud
Security in the cloudSecurity in the cloud
Security in the cloud
 
2. migration, disaster recovery and business continuity in the cloud
2. migration, disaster recovery and business continuity in the cloud2. migration, disaster recovery and business continuity in the cloud
2. migration, disaster recovery and business continuity in the cloud
 
1. aws overview
1. aws overview1. aws overview
1. aws overview
 
Pgp
PgpPgp
Pgp
 
ssl for securing
ssl for securingssl for securing
ssl for securing
 
03 unity 3_d_part_2
03 unity 3_d_part_203 unity 3_d_part_2
03 unity 3_d_part_2
 
02 unity 3_d_part_1
02 unity 3_d_part_102 unity 3_d_part_1
02 unity 3_d_part_1
 
01 unity 3_d_introduction
01 unity 3_d_introduction01 unity 3_d_introduction
01 unity 3_d_introduction
 
unity basics
unity basicsunity basics
unity basics
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Recently uploaded (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 

Jar chapter 5_part_ii

  • 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 S.W. Lau and Y.K. Leung McGraw-Hill Education (Asia), 2005
  • 2. 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.
  • 3. 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.
  • 5. 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” } } }
  • 6. 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).
  • 7. 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.
  • 8. 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: … … } } 
  • 9. 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
  • 10. 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.
  • 11. 11 Example – Control Object of Vending Machine
  • 12. 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;
  • 13. 13 Example – Control Object of Vending Machine (cont’d) public VendingMachineControl(float price) { _amount = 0; _state = WaitingCoin; _price = price; }
  • 14. 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
  • 15. 15 Example – Implementing a State Diagram with Sequential Substates
  • 16. 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; }
  • 17. 17 Example – Implementing a State Diagram with Sequential Substates (cont’d) public boolean dispensedSoftDrink() { if (_state == DispensingSoftDrink) { _state = DispensingChange; dispense change; } return false; }
  • 18. 18 Example – Implementing a State Diagram with Sequential Substates (cont’d) public boolean dispensedChange() { if (_state == DispensingChange) { _state = Complete; return true; } return false; }
  • 19. 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(); }
  • 20. 20 Example – Implementing a State Diagram with Sequential Substates (cont’d) public void dispensedSoftDrink() // VendingMachineControl { if (_state == Dispensing) { boolean isComplete = _substate.dispensedSoftDrink(); } }
  • 21. 21 Example – Implementing a State Diagram with Sequential Substates (cont’d) // VendingMachineControl public boolean dispensedChange() { if (_state == Dispensing) { boolean isComplete = _substate.dispensedChange(); if (isComplete) { amount = 0; _state = WaitingCoin; } } }
  • 22. 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.
  • 23. 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.
  • 24. 24 Sequence Diagrams (cont’d) ClassB { ClassC o3; ClassD o4; method1(…) { o3.method2(..); o4.method3(..); } }