0% found this document useful (0 votes)
83 views

ICT340 Seminar 4

This document discusses implementing application designs by writing code. It explains that code should be written based on the structural model and dynamic models, including associations identified through walkthroughs and methods from sequence diagrams. As an example, it analyzes the "toRent" association between the Car and Contract classes. For this association, it identifies that the Car class should be modified, the association being implemented is "toRent", navigation is from Car to Contract, the multiplicity is 1 to 0 or 1, an instance variable for the contract should be added to the Car class, and its value would be either null or a Contract object. The document provides an example of how this would look in Java code.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

ICT340 Seminar 4

This document discusses implementing application designs by writing code. It explains that code should be written based on the structural model and dynamic models, including associations identified through walkthroughs and methods from sequence diagrams. As an example, it analyzes the "toRent" association between the Car and Contract classes. For this association, it identifies that the Car class should be modified, the association being implemented is "toRent", navigation is from Car to Contract, the multiplicity is 1 to 0 or 1, an instance variable for the contract should be added to the Car class, and its value would be either null or a Contract object. The document provides an example of how this would look in Java code.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Official (Closed) - Non Sensitive

ICT340
Application Analysis and Design

Implementations (Unit 5)

1
Official (Closed) - Non Sensitive

Implementations

Writing the codes for the design from:


1 The structural model
2 The dynamic model
(a) associations from walkthrough
(b) methods from sequence diagrams

2
Official (Closed) - Non Sensitive

Implementations
Recall the structural model

Analyse the user requirements for:


• classes
• attributes
Part 1
• generalisation relationships
• associations
Part 2
• multiplicities of associations

3
Official (Closed) - Non Sensitive

Implementations

From the structural model

class: Student
name: String
address: String
telephone: long

. . . // methods

4
Official (Closed) - Non Sensitive
public class Student
{ private String name;
Implementations private String address;
private int telephone;

public Student (String name, String address,


The usual Java code int telephone)
{ this.name = name;
this.address = address;
this.telephone = telephone;
}
public String getName()
{ return name;
}
public void setName(String name)
{ this.name = name;
}
public String getAddress()
{ return address;
}
public void setAddress(String address)
{ this.address = address;
}
5
. . .
}
Official (Closed) - Non Sensitive

Implementations class Student(object):


def __init__(self, newName, newAddress, \
newTelephone):
The usual Python code self.__name = newName
self.__address = newAddress
self.__telephone = newTelephone

public String getName(self):


return self.__name;

public void setName(self, newName):


self.__name = newName;

public String getAddress(self):


return self.__address;

public void setAddress(self, newAddress):


self.__address = newAddress;

. . .
6
Official (Closed) - Non Sensitive

Implementations

Writing the codes for the design from:


1 structural model
2 The dynamic model
(a) associations from walkthrough
(b) methods The from sequence diagrams

7
Official (Closed) - Non Sensitive

Implementations
After all the walkthroughs . . .
Admin
hasCustomers 1 1 hasBranches

* hasManagers
Employee
works in

* 1 *
1 1
BranchWorker Manager Branch
isResponsibleFor
1 has
* 0..*
signs toRent
Customer Contract Car
1 1 0, 1 1..* 8
Official (Closed) - Non Sensitive

Implementations As an example, take one association . . .

Admin
hasCustomers 1 1 hasBranches

* hasManagers
Employee
works in

* 1 *
1 1
BranchWorker Manager Branch
isResponsibleFor
1 has
* 0..*
signs toRent
Customer Contract Car
1 1 0, 1 1..*
9
Official (Closed) - Non Sensitive

Consider 1
Implementations direction at a
time
Recall . . . toRent
Contract Car
0, 1 1..*

Class being modified Car


Association being implemented toRent
Direction of navigation from Car to Contract
Multiplicity 1 to 0, or 1 to 1
Instance variable added contract
Value of the instance variable either null or
a Contract object
10
Official (Closed) - Non Sensitive

Implementations
toRent
Contract Car
0, 1 1..*

class: Car
regNo: String
contract: Contract
...

. . . // methods

Look at the multiplicity


11
Official (Closed) - Non Sensitive

Implementations – Java
toRent
Contract Car
0, 1 1..*

public class Car


{ ... class: Car
private Contract contract;
regNo: String
... contract: Contract
public Contract getContract() ...
{ return contract;
} . . . // methods
public void setContract(Contract c)
{ this.contract = c;
}
...
} The code is straightforward . . .
12
Official (Closed) - Non Sensitive

Implementations – Python
toRent
Contract Car
0, 1 1..*

class Car(object) class: Car


def __init__(self, nRegNo, nContract): regNo: String
self.__regNo = nRegNo contract: Contract
self.__contract = nContract ...
def getContract(self): . . . // methods
return self.__contract

def setContract(self, nContract):


self.__contract = nContract; The code is straightforward . . .

... 13
Official (Closed) - Non Sensitive

Implementations
The other direction . . .

toRent
Contract Car
0, 1 1..*

Class being modified Contract


Association being implemented toRent
Direction of navigation from Contract to Car
Multiplicity one to many
Instance variable added cars
Value of the instance variable a HashMap (or dict) object
14
Official (Closed) - Non Sensitive

Implementations
toRent
Contract Car
0, 1 1..*

class: Contract
duration: int
cars: HashMap
...

. . . // methods

Look at the multiplicity


15
Official (Closed) - Non Sensitive

Implementations – Java
toRent
Contract Car
0, 1 1..*

Recall collections
public class Contract
{ ...
class: Contract private HashMap cars; Why is a setter not
duration: int appropriate here?
...
cars: HashMap int getNumOfCar() { . . . }
... void addCar(Car aCar) { . . . }
boolean hasCar(Car aCar) { . . . }
. . . // methods Car searchCars(String regnNo) { . . . }
void listCar() { . . . }
boolean removeCar(String regnNo) { . . . }
...
}
16
Official (Closed) - Non Sensitive

Implementations – Python
toRent
Contract Car
0, 1 1..*
Recall collections
class Contract(object):
def __init__(self, nStartDate, nDuration):
self.__cars = {}
class: Contract ...
def getNumOfCar(self) :
duration: int ...
cars: HashMap def addCar(self, aCar):
... ...
def hasCar(self, aCar):
. . . // methods ...
def searchCars(self, regnNo):
...
def listCar(self) :
...
17
def removeCar(self, regnNo):
Official (Closed) - Non Sensitive

Implementations

Writing the codes for the design from:


1 The structural model
2 The dynamic model
(a) associations from walkthrough
(b) methods from sequence diagrams

18
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
assignManager(
location,
employeeName, branch1 =
employeeNumber) searchBranches(location) A sequence
diagram
4
aManager = new Manager
(employeeName, employeeNumber)
aManager:Manager

assignManager 3 setManager(aManager)
(aManager)

setBranch(branch1) 1
19
Official (Closed) - Non Sensitive

Remember the walkthrough?

Admin
hasCustomers 1 1 hasBranches

* hasManagers
Employee
works in

* 1 *
1 1
BranchWorker Manager Branch
isResponsibleFor
1 has
* 0..*
signs toRent
Customer Contract Car
1 1 0, 1 1..*
20
Official (Closed) - Non Sensitive

Implementations – Review basic OO programming

Before we look at sequence


diagrams, a review of basic A message aCircle: Circle
OO programming. . . findArea() radius = 3
...
public double findArea()
{ . . .
}
. . .

Who receives the message?


Whose method is activated?
Who has to have the method for this message?
21
Official (Closed) - Non Sensitive

Implementations – Review basic OO programming

class: SomeOtherClass
...
public void someMethod()
{ . . . aCircle: Circle
aCircle = new Circle(3);
ans = aCircle.findArea(); radius = 3
. . . ...
}
. . . // other methods
public double findArea()
{ . . .
}
. . .

Who is sending the message?

22
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
assignManager(
location,
employeeName, branch1 =
employeeNumber) searchBranches(location) A sequence
diagram
4
aManager = new Manager
(employeeName, employeeNumber)
This is from the previous session. aManager:Manager
Did you recall how we drew this?
Remember why we have this arrow doing a “U-turn”
assignManager 3 setManager(aManager)
(aManager)

setBranch(branch1) 1
23
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5 It is easier to start
from the bottom!
assignManager(
location,
employeeName, branch1 =
employeeNumber) searchBranches(location) What method is
used here?
4
aManager = new Manager
(employeeName, employeeNumber)
aManager:Manager

assignManager 3 setManager(aManager)
(aManager)
Who is receiving
2 the message?

Who is sending 1
setBranch(branch1)
the message? 24
Official (Closed) - Non Sensitive

Implementations
Admin
hasCustomers 1 1 hasBranches

* hasManagers
Employee
works in
How is it related
to this?
* 1 *
1 1
BranchWorker Manager Branch
isResponsibleFor
1 has
* 0..*
signs toRent
Customer Contract Car
1 1 0, 1 1..*
25
Official (Closed) - Non Sensitive

Implementations
Recall . . . isResponsibleFor
Manager Branch
1 1

public class Manager


{ ... class: Manager
private Branch aBranch;
aBranch: Branch
...
public Branch getBranch()
...
{ return aBranch;
}
public void setBranch(Branch b) . . . // methods
{ this.aBranch = b;
}
...
}
26
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
assignManager(
location,
employeeName, branch1 =
employeeNumber) searchBranches(location) What method is
used here?
4
aManager = new Manager
(employeeName, employeeNumber)
Who is sending aManager:Manager
the message?

assignManager 3 setManager(aManager)
(aManager)

2
Who is receiving
the message? setBranch(branch1) 1
27
Official (Closed) - Non Sensitive

Implementations
Admin
hasCustomers 1 1 hasBranches

* hasManagers
Employee
works in
How is it related
to this?
* 1 *
1 1
BranchWorker Manager Branch
isResponsibleFor
1 has
* 0..*
signs toRent
Customer Contract Car
1 1 0, 1 1..*
28
Official (Closed) - Non Sensitive

Implementations
Recall . . . isResponsibleFor
Manager Branch
1 1

In Java In Python
public class Branch class Branch(object):
{ ... class: Branch def __init__(self, nManager):
private Manager aManager; self.__aManager = nManager
...
aManager: Manager
...
public Manager getManager() ... def getManager(self):
{ return aManager; return self.a__Manager
}
public void setManager(Manager m) def setManager(self, m):
{ this.aManager = m;
. . . // methods self.__aManager = m
}
... ...
}
29
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
assignManager(
location,
employeeName, branch1 =
employeeNumber) searchBranches(location) What will the
assignManager()
4 method consist of?

aManager = new Manager


Who(employeeName,
is sending employeeNumber)
the message? aManager:Manager

assignManager 3 setManager(aManager)
(aManager)

Who is receiving
2
the message?

setBranch(branch1) 1
30
Official (Closed) - Non Sensitive

Implementations – Java

Whose method is this?


Look at these

public void assignManager(aManager) // 3


{ this.setManager(aManager); // 2
aManager.setBranch(this); // 1
}

Who is to receive the message for this method?


What does "his" refer to?
31
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
assignManager(
location,
employeeName, branch1 =
Who is using constructor
employeeNumber) searchBranches(location) to create a manager?

4
aManager = new Manager
(employeeName, employeeNumber)
aManager:Manager

assignManager 3 setManager(aManager)
(aManager)

setBranch(branch1) 1
32
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
Who is sending
assignManager( the message? What method
location, is used here?
employeeName, branch1 =
employeeNumber) searchBranches(location)

Who is receiving aManager = new Manager


the message? (employeeName, employeeNumber)
aManager:Manager

assignManager 3 setManager(aManager)
(aManager)

setBranch(branch1) 1
33
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5
Who is receiving
assignManager( the message?
What will be done when this
location, message is received?
employeeName, branch1 =
employeeNumber) searchBranches(location)
What does this assignManager()
4 consist of ?
Who is sending aManager = new Manager
the message? (employeeName, employeeNumber)
aManager:Manager

assignManager 3 setManager(aManager)
(aManager)

setBranch(branch1) 1
34
Official (Closed) - Non Sensitive

Write the method from


Implementations – Java the sequence diagram

public void assignManager(String location,


String employeeName, String employeeNumber)
{
Branch branch1 = this.searchBranches(location);

Manager aManager = new Manager(employeeName,


employeeNumber);

branch1.assignManager(aManager);
}
Who is to receive the message for this method?
What does "this" refer to?
This is actually the method sent to the orchestrating object.
35
How does the whole thing work?
Official (Closed) - Non Sensitive

Write the method from


Implementations – Python the sequence diagram

def assignManager(self, location, employeeName, \


employeeNumber):

branch1 = self.searchBranches(location)

aManager = Manager(employeeName, employeeNumber)

branch1.assignManager(aManager);
}

36
Official (Closed) - Non Sensitive
:Admin branch1:Branch
5 How the whole
Message thing work?
assignManager(
location,
employeeName, branch1 =
employeeNumber) searchBranches(location)
Responses to
A the message
4
aManager = new Manager
(employeeName, employeeNumber)
B aManager:Manager

C
assignManager 3 setManager(aManager)
(aManager)
public void assignManager(String location,
String employeeName,
2 String employeeNumber)
{
Branch branch1 = this.searchBranches(location);
setBranch(branch1)
A1
aManager = new Manager(employeeName, employeeNumber);
B 37
branch1.assignManager(aManager); C
Official (Closed) - Non Sensitive

Implementations

Writing the codes for the design from:


1 The structural model
2 The dynamic model
(a) associations from walkthrough
(b) methods from sequence diagrams

What has happened?


What have we done?
38
Official (Closed) - Non Sensitive

Gentle Reminders
This is a practical course
Reading and memorising will not work
Must do the exercises
Ask yourself
• “What is happening here?”
• “Why is this done?”

39
Official (Closed) - Non Sensitive

What we have covered

• Write code for classes from structural (static) modelling


• Add code to classes from walkthroughs in dynamic
modelling
• Add code to classes from sequence diagrams in dynamic
modelling

40

You might also like