Week 13 student directed learning implementation

Page 1

IT1573

Week 13 Student Directed Learning Mapping Design to (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. Check the deadline for submission in Blackboard.


DBT 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

8.

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

9.

Mapping Associations ..................................................................................................................... 7 9.a.

Mapping Uni-Directional one-to-many association ................................................................ 8

9.b.

Mapping a bi-directional one-to-many association ................................................................ 9

10.

Composition and Aggregation. ................................................................................................. 10

11.

Many-to-Many association relationship ................................................................................... 11

12.

Conclusion ................................................................................................................................. 13

Page 1 of 14


DBT School of Information Technology, Nanyang Polytechnic Student Directed Learning (SDL) Use Case Realization (Part 3) Mapping Model to Code

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

Explain what forward engineering is Explain how the various UML models supports forward engineering Map the various design class diagrams into a programming language such as Java

You are also required to submit the outcome of the tasks expected in this SDL to the Q:\ drive using the assignment submission application to your respective groups. This submission will contribute 5% to the module. The tasks are indicated by this picture:

There are 4 tasks in total.

The topics and concepts covered in this self-learning working WILL 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


DBT 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 or 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.

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 high-level abstractions and logical, implementationindependent designs 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 shows how these models support the process of forward engineering.

Page 3 of 14


DBT School of Information Technology, Nanyang Polytechnic

This approach is the norm if we are developing a totally new system. That is, there is no existing system and the software engineer must starts from scratch from the beginning. Software engineers will use CASE (Computer Aided Software Engineering) tools just as 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 to the architecture and the structure of the existing system? In this case we can apply a process called Reverse Engineering

Or the 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 revisions and enhancement

Page 4 of 14


DBT School of Information Technology, Nanyang Polytechnic 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 :

Hence the round-trip engineering 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 evolved 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:

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

Page 5 of 14


DBT School of Information Technology, Nanyang Polytechnic 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 class is fairly straightforward; the attributes becomes the class instance variables and the operations are mapped into Java methods. Assuming we have derived the following solution classes after our system design stage. In the solution class diagram shown below, BasicAccount is the parent or super class and InterestAccount is the child or sub class. BasicAccount has 1 attribute and 3 operations and InterestAccount has 1 attribute and 2 operations.

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

public class BasicAccount { private double balance; public double getBalance() { …… } public void credit(double amount) { …… } public void debit(double amount) { …… } } What is to be included in the method {……} have to be figured out by you. The software tool cannot generate these for you.

Page 6 of 14


DBT School of Information Technology, Nanyang Polytechnic Task 1: Map the following Student solution class to a Java 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. In Java, we use the “extends” keyword in Java to indicate inheritance relationship between two classes.

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

9. Mapping Associations Association in a solution class diagram can be viewed as navigability. A typical uni-directional association requires the one object of a class to know about the other object(s) of another class. A uni-directional 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 7 of 14


DBT School of Information Technology, Nanyang Polytechnic

9.a.

Mapping Uni-Directional one-to-many association

The above solution class diagram shows a uni-directional (from ProductCategory to Item) association. This means that ProductCategory knows about all items BUT items do know have knowledge about its category. Mapping the Item class is fairly straightforward since it does have any knowledge of any other class of objects. public class Item{ private string itemID; private int UnitPrice; public string getItemID(){ …… }

}

public int getItemPrice() { ……}

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

public class ProductCategory{ private string category; private ArrayList<Item> listOfItems; As you can see, it is sometimes useful to have role names for the class in the solution class diagram. In this example the Item class has the role of listOfItems in association with the ProductCategory class. (See Week 12 Lecture Slide no 13).

public void addItem(Item item){ listOfItems.add(item); } public void listItems() { ……} public string getCategory(){ ……} Page 8 of 14


DBT School of Information Technology, Nanyang Polytechnic Hence any product category object has knowledge about its items in its category. Now if you are given an item object, is there any way to find out about which category it belongs to? The simple answer is NO. This is because the association is only uni-directional, navigating only from the product category object. Then what do we need to do if we want to allow each item to know about its category? The answer is to make the association bi-directional.

9.b.

Mapping a bi-directional one-to-many association

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 item is associated to 1 category object. We have to revise our implementation of Item to reflect this:

public class Item{ private string itemID; private int UnitPrice; private ProductCategory category; public string getItemID(){ …… }

}

public int getItemPrice() { ……}

As you can see the code remains largely the same, except now, we have a new variable (an object of the ProductCategory class) in the Item class called category. We won’t expect you to provide the coding for the method body. That is beyond the scope of this module.

Page 9 of 14


DBT School of Information Technology, Nanyang Polytechnic 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.

10.

Composition and Aggregation. Mapping composition and aggregation relationships are familiarly similar to unidirectional association.

In the above examples, only the container class (i.e. Class8 and Class10) ‘knows’ about the contained class (i.e. Class9 and Class11). So the navigation is from the container class to the contained class. Example:

public class Car { private String make; private String model; private String manufacturer; private Engine engine;

As whole and part must co-exist, the part will be created together with the whole. Hence to model the composition, we make use of the constructor of Car to create the Engine object. (Refer to Week 13 Lecture Slide 9 for more detials)

public Car (String make, String model, String manufacturer, String engineType, double cap) { …….. engine =new Engine(engineType, cap); } public String getMake () { … } public String getModel () { … } public String getManufacturer () { … } }

Page 10 of 14


DBT School of Information Technology, Nanyang Polytechnic The mapping of aggregation is similar to composition. If the composition is changed to aggregation, then we can have the Engine object created outside the Car object first then pass it into the constructor of Car.

public class Car { private String make; private String model; private String manufacturer; private Engine engine;

Engine object e is created beforehand and passed into Car constructor

public Car (String make, String model, String manufacturer, Engine e) { … engine =e; }

(Refer to Week 13 Lecture Slide 11 for more details)

public String getMake () { … } public String getModel () { … } public String getManufacturer () { … } }

Task 3: Given the following solution class diagram, map them into corresponding Java class templates.

11.

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

Page 11 of 14


DBT School of Information Technology, Nanyang Polytechnic 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:

In the context of the database, the ModuleRegistration is known as an association or 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 modeled 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:

Page 12 of 14


DBT School of Information Technology, Nanyang Polytechnic 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:

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 4: You are given the following design solution class diagram, map the design into an corresponding 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, associations, aggregation and composition class relationships to Java class templates. You should also know now that a many-to-many association relationships normally involve an association class and this manyto-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 manyto-many association.

Page 13 of 14


DBT School of Information Technology, Nanyang Polytechnic You should have completed the 4 tasks in this SDL. Submit your solution to these 4 tasks in a separate word document.

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.