SENGP - Mapping Design to Code

Page 1

IT2604/IT2755/IT2632/IT2535 Week 12 Student Directed Learning Mapping Design to (C#/Java) Code

This worksheet forms the work that you have to do during week 12 SDL week. You are to read and work through this worksheet and complete a mini-assignment (which consists of a number of tasks) which you have to submit. This submission contributes to 5% of the module. Deadline for submission is 4th January 2013, 5 pm.


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

Table of Contents 1.

Learning outcomes.......................................................................................................................... 2

2.

Recap............................................................................................................................................... 2

3.

Forward Engineering. ...................................................................................................................... 3

4.

Reverse Engineering........................................................................................................................ 4

5.

Round-Trip Engineering .................................................................................................................. 5

6.

Relationships between Solution classes ......................................................................................... 5

7.

Mapping a solution class ................................................................................................................. 6

7.a.

Mapping a solution class to C# .................................................................................................. 6

7.b.

Mapping a solution class to Java................................................................................................. 7

8.

Mapping Inheritance (Generalisation/Specialisation) relationship ................................................ 7 8.a.

Mapping Inheritance in C# ...................................................................................................... 8

8.b.

Mapping Inheritance in Java ................................................................................................... 8

9.

Mapping Uni-directional Associations ............................................................................................ 8 9.a.

Mapping Uni-Directional one-to-many association (C#) ........................................................ 9

9.b.

Mapping Uni-Directional one-to-many association (Java).................................................... 10

10.

Mapping Bi-directional Associations......................................................................................... 11

10.a. Mapping a bi-directional one-to-many association (C#) ...................................................... 11 10.b.

Mapping a bi-directional one-to-many association (Java) ................................................ 11

11.

Many-to-Many association relationship ................................................................................... 12

12.

Conclusion ................................................................................................................................. 14

Page 1 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic Student Directed Learning (SDL) Week 12 Use Case Realization Implementation Mapping Model to Code

1. Learning outcomes. After this SDL, you will be able to: • • •

Explain the various software engineering approaches are Explain how the various UML models supports forward engineering Map the various design class diagrams into a programming language such as C# or Java

You are also required to submit the outcome of the tasks expected in this SDL as instructed by your module leader/tutor. This submission will contribute 5% to the module. The tasks are indicated by this picture:

There are 3 tasks in total. The topics and concepts covered in this self-learning working MAY BE tested in the upcoming written test 2 and the end of module exam.

2. Recap. So far you have learnt the various models to model the different aspects of a software system: Use case model and description: Model the system functional requirements and the various scenarios and process of each of the system functions.

Domain model (Domain class diagram):

Model the system domain objects that the system wants to track and their relations.

Page 2 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

Sequence Diagram (interaction diagram)

Model the interaction between the various system solution objects. This will help us derive the responsibilities of each of the solution objects.

Design model (Solution class diagram) Model the system solution objects (i.e. the software components) which make up the system.

Each of the above models has their purpose. Ultimately each of these models is bringing us closer to our goal – to realise the software system. The next step after the design model we will map the model into actual programming code. The next few sections explain the different engineering approaches to developing a computer system.

3. Forward Engineering. What we have been doing to develop an information system is known as Forward Engineering. Forward Engineering is formally defined as: The traditional process of moving from a logical, high-level abstraction and implementationindependent design to the physical implementation of a system. What this means is that we produced started from conceptual analysis and design of a system using various modelling techniques to model the different aspects of a system with the aim of coming up with a solution to implement a working software system. Section 2 previously showed how these models support the process of forward engineering.

Page 3 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

This approach is the norm if we are developing a totally new system. That is, when there is no existing system and the software engineer must starts from scratch and from the beginning (aka Greenfield Engineering). Software engineers will use CASE (Computer Aided Software Engineering) tools like Visual Paradigm or IBM Rational Architect to create the various models which you have learnt so far in this module. These tools also have the capability to generate skeletal code from the Design Model.

4. Reverse Engineering But what if there is an existing computer system already and your client wants your software company to re-develop or to revamp system? But there are no available software models of the existing system and all you have is the code? How can you get to learn the architecture and the structure of the existing system? In this case we can apply a process called Reverse Engineering

In another scenario, the existing system might be fairly old. In this case the existing old model might not match the existing state of the coding as the system might have undergone a number of

Page 4 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic revisions and enhancement and these changes might not have been updated in the original software models and hence it has become out of sync with the source code.

5. Round-Trip Engineering There is also another approach called the Round-Trip Engineering :

The round-trip engineering approach will sync the model to the code if changes are done to the code or it will generate ‘new’ code if modifications are done on the various software models. Therefore, the key characteristic of round-trip engineering that distinguishes it from forward and reverse engineering is the ability to synchronize existing artefacts (such as the various software models and their corresponding codes) that evolve concurrently by incrementally updating each artefact to reflect changes made to the other artefacts. Round-trip engineering is critical for maintaining consistency among multiple models and between the models and the code.

6. Relationships between Solution classes We have already seen the various relationships that can exist between the classes in a UML domain class diagram. These relationships can also exist between the solution classes in the solution class diagrams:

Page 5 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic Once you have obtained your solution class diagram (that is you have defined the data types of the class attributes, operation names and their respectively parameter lists and data types and the operations return types), you can use the individual classes and map it into a skeletal code based on an object oriented programming language such as Java, C++, C# or VB. Professional edition of Visual Paradigm and IBM Rational Architecture can auto generate these for you. However for this SDL, you will see how it can be done manually. We will use the Java programming language to demonstrate the mapping of the solution classes and their relationships.

7. Mapping a solution class Mapping and coding an individual solution class to a corresponding Java, VB, C++ or C# class is fairly straightforward; the attributes becomes the class instance variables and the operations are mapped into methods/operations. Assuming we have derived the following solution classes after our system design stage. In the solution class diagram shown below, BasicAccount is the parent, super or base class and InterestAccount is the child, sub or derived class. BasicAccount has 2 attributes and 3 operations and InterestAccount has 1 attribute and 2 operations.

Let’s see how we can map the BasicAccount class first.

7.a. Mapping a solution class to C# public class BasicAccount { private string accountNumber; private double balance; public double getBalance() { …… } public void credit(double amt) { …… } public void debit(double amt) { …… } } Page 6 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

7.b. Mapping a solution class to Java

What is to be included in the method {‌‌} have to be completed by the software developers. The software tool cannot generate these for you.

Task 1: Map the following Student solution class to a C# class template.

Student - id : String - nam e : Stri ng + Student(id : String, name : Stri ng) + hasPrerequsite() : Bool ean

8. Mapping Inheritance (Generalisation/Specialisation) relationship

To map and code the InterestAccount class, we have to map also the inheritance relationship.

Page 7 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

8.a.

Mapping Inheritance in C#

8.b.

Mapping Inheritance in Java

In Java, we use the “extends” keyword in Java to indicate inheritance relationship between two classes.

public class InterestAccount extends BasicAccount { private double interestRate; public void setInterestRate(double r) { …… } public void payInterest() { …… } }

9. Mapping Uni-directional Associations Association in a solution class diagram can be viewed as navigability. A typical uni-directional association requires one object of a class to “know” about object(s) of another class. A unidirectional association only requires the possibility of navigation from one object to another object. Likewise, a bi-directional association requires that both objects may “traverse” (move) to each other.

Page 8 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

The above solution class diagram shows a uni-directional (from BasicAccount to Transaction) association. This means that BasicAccount knows about all Transaction BUT Transaction does know have knowledge about its BasicAccount.

9.a. Mapping Uni-Directional one-to-many association (C#) Mapping the Transaction class is fairly straightforward since it does have any knowledge of any other class of objects. public class Transaction{ private int txID; private Date txDate; private double txAmt; private string txType;

}

public double getAmt(){…… } public string getTxType() {………} public Date getTxDate() {……}

Now we have to map the BasicAccount class. Since we can navigate from BasicAccount to Transaction, we have to map that association in the BasicAccount class. To do this we have to include a list of transactions in the BasicAccount class (since an account can have many transactions). To define a list of Transaction object in C#, we can use and array ([]):

Page 9 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

9.b. Mapping Uni-Directional one-to-many association (Java) Mapping the Transaction class is fairly straightforward since it does have any knowledge of any other class of objects. public class Transaction{ private int txID; private Date txDate; private double txAmt; private String txType;

}

public double getAmt(){…… } public String getTxType() {………} public Date getTxDate() {……}

Now we have to map the BasicAccount class. Since we can navigate from BasicAccount to Transaction, we have to map that association in the BasicAccount class. To do this we have to include a list of transactions in the BasicAccount class (since an account can have many transactions). To define a list of objects in Java, we can use and array ([]) or an ArrayList<>:

Hence any basic account object has knowledge about its transactions. Now if you are given an transaction object, is there any way to find out about which account this transaction object belongs to? The simple answer is NO. This is because the association is only uni-directional, navigating only from the basic account object. Then what do we need to do if we want to allow each transaction to know about its account? The answer is to make the association bi-directional.

Page 10 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

10.

Mapping Bi-directional Associations Basically the solution class diagram looks the same but there are no arrow head to show direction, by convention, absence of arrow head implies bi-directional association.

So every transaction is associated to 1 basic account object. We have to revise our implementation of Item to reflect this.

10.a. Mapping a bi-directional one-to-many association (C#)

10.b.

Mapping a bi-directional one-to-many association (Java)

Page 11 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic As you can see the code remains largely the same, except now, we have a new variable (an object of the BasicAccount class) in the Transaction class called acct. We won’t expect you to provide the coding for the method body. That is beyond the scope of this module. Task 2: Construct a class diagram that is equivalent to the code given below. The software classes derived must have the necessary details and the appropriate relationship among them. Java Code:

C# Code: public class Lecturer : Staff { private Module [ ] moduList; …. }

11.

public class Module { private string moduleCode; private Lecturer tutor; public boolean computeGrade(){ …} ….

Many-to-Many association relationship

If we have a many-to-many association relationship in our solution class diagram, we cannot map this design directly. What we must do first to resolve this single many-to-many relationship into 2 separate 1-to-many and many-to-1 associations, just as you would in database design when there is a many-to-many relationship between two tables. In the context of database you would create an association table to separate the many-to-many relationship into two 1-to-many (from one table to the association table) and many-to-one (from the association table to the other table) as illustrated below:

Page 12 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic In the context of the database, the ModuleRegistration is known as an association or an intersection table. In UML class diagrams (domain model as well as solution model) an association class can also be used to model an association as a class. Association classes often occur in many-to-many associations especially where the association itself has attributes. An association class is rendered by a dashed line from the association to the class rectangle. Each link in the association is an object of the association class. An association class is essentially a class attached to an association; the association itself is modelled as a class. For example:

You probably have seen the above examples umpteen times. A student can study many modules and a module can be studied by many students But how can we relates the modules studied by a particular students or know which students are studying a particular module? In this case we can model the association as an association class and put that information in the association class as shown below:

Distinguish between the use of an association class as a modeling technique and the implementation of the association class. There can be several ways to implement an association class. The easy way is to change the many-to-many association and the association class to two unidirectional 1-to-many and many-to-1 associations as shown below:

Page 13 of 14


DIS/DBI/DFI/DEI School of Information Technology, Nanyang Polytechnic

Once you have resolve the many-to-many association to 2 separate uni-directional one-tomany and many-to-one associations, the mapping will proceed as described in Section 9a. You can also refer to Week 12 lecture slides no 14 and 15 for another more detailed example. Task 3: You are given the following design solution class diagram, map the design into a corresponding C#/Java class templates.

12.

Conclusion

In this SDL, you have learnt what is meant by forward engineering and how the various UML models support this. You have also learnt how to map inheritance and associations class relationships to C#/Java class templates. You should also know now that a many-to-many association relationships normally involve an association class and this many-to-many association has to be resolved into two 1-to-many and many-to-1 associations with the association class as the new class between the two classes involved in the original many-tomany association.

Page 14 of 14


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.