Successfully reported this slideshow.

17 Mapping Design To Code

7,262 views

Published on

Published in: Technology
  • Be the first to comment

17 Mapping Design To Code

  1. 1. TK2023 Object-Oriented Software Engineering CHAPTER 17 Mapping Designs to Code
  2. 2. INTRODUCTION <ul><li>The interaction diagrams and design class diagrams created during design provide some of the necessary input for generating code. </li></ul><ul><li>In this chapter, we will see how to map those artifacts to code in an object-oriented language. </li></ul>
  3. 3. <ul><li>The following interaction and class diagrams will be used to show the mapping process: </li></ul>: Register enterItem(itemID, qty)) : Sale sli : SalesLineItem { new } 2: makeLineItem(psList[i] ,qty) 2.1: SalesLineItem(psList[i] ,qty) 2.2: add(sli) : ProductCatalog 1: psList[i] = getProductDesc(itemID) 1.1: psList[i] = find(itemID) {new} : ArrayList<SalesLineItem> psList : ArrayList<ProductDescription> psList[i] : ProductDescription
  4. 4. Register enterItem(itemID,qty) ProductCatalog getProductDesc(itemID) Sale makeLineItem(desc,qty) quantity SalesLineItem ProductDescription itemID price description 1 1..* 1 1..* 1 currentSale lineItems description catalog descriptions
  5. 5. CREATING CLASS DEFINITIONS FROM DESIGN CLASS DIAGRAMS <ul><li>Basic class definitions can be written from the design class diagrams. The following information can be extracted: </li></ul><ul><ul><li>Class name </li></ul></ul><ul><ul><li>Attributes: name, type and access specifier </li></ul></ul><ul><ul><li>Method: name, return type, parameters and their types, and its access specifier </li></ul></ul>
  6. 6. <ul><li>Example: </li></ul>class Sale { private Vector lineItems; public void makeLineItem(ProductDescription desc, int qty) { } } Sale makeLineItem(desc,qty) quantity SalesLineItem ProductDescription itemID price description 1..* 1 lineItems description <ul><li>class SalesLineItem { </li></ul><ul><ul><li>private int quantity; </li></ul></ul><ul><ul><li>private ProductDescription description; </li></ul></ul><ul><ul><li>public SalesLineItem(ProductDescription pd, int qty) { } </li></ul></ul><ul><li>} </li></ul><ul><li>class ProductDescription { </li></ul><ul><ul><li>private ItemID itemID; </li></ul></ul><ul><ul><li>private Money price; </li></ul></ul><ul><ul><li>private String description; </li></ul></ul><ul><li>} </li></ul>
  7. 7. <ul><li>Note that the constructor in the class SalesLineItem is derived from the creation of the SalesLineItem object in the interaction diagram. </li></ul>: Sale sli : SalesLineItem { new } 2.2: add(sli) : ArrayList<SalesLineItem> 2.1: SalesLineItem(pd ,qty)
  8. 8. CREATING METHODS FROM INTERACTION DIAGRAMS <ul><li>The sequence of messages in an interaction diagram translates to a series of statements in the method definitions. </li></ul>
  9. 9. <ul><li>For example, consider writing the method definition for enterItem (). </li></ul>: Register : ProductCatalog 1: pd = getProductDesc(itemID) enterItem(itemID, qty)) : Sale 2: makeLineItem(pd,qty) <ul><li>class Register { </li></ul><ul><ul><li>private Sale currentSale; </li></ul></ul><ul><ul><li>private ProductCatalog catalog; </li></ul></ul><ul><ul><li>public void enterItem(ItemID itemID, int qty) { </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  10. 10. : Register : ProductCatalog 1: pd = getProductDesc(itemID) enterItem(itemID, qty)) : Sale 2: makeLineItem(pd,qty) <ul><li>class Register { </li></ul><ul><ul><li>private Sale currentSale; </li></ul></ul><ul><ul><li>private ProductCatalog catalog; </li></ul></ul><ul><ul><li>public void enterItem(ItemID itemID, int qty) { </li></ul></ul><ul><ul><li>ProductDescription pd; </li></ul></ul><ul><ul><li>pd = catalog.getProductDesc(itemID); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  11. 11. : Register : ProductCatalog 1: pd = getProductDesc(itemID) enterItem(itemID, qty)) : Sale 2: makeLineItem(pd,qty) <ul><li>class Register { </li></ul><ul><ul><li>private Sale currentSale; </li></ul></ul><ul><ul><li>private ProductCatalog catalog; </li></ul></ul><ul><ul><li>public void enterItem(ItemID itemID, int qty) { </li></ul></ul><ul><ul><li>ProductDescription pd; </li></ul></ul><ul><ul><li>pd = catalog.getProductDesc(itemID); </li></ul></ul><ul><ul><li>currentSale.makeLineItem(pd, qty); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  12. 12. COLLECTION CLASSES <ul><li>One-to-many relationships are common. For example, a Sale is associated with a group of SalesLineItem objects. </li></ul><ul><li>In OO programming languages, these relationships are usually implemented using collection objects such as Vectors, Lists, Maps, arrays and so on. </li></ul>
  13. 13. <ul><li>For example, consider </li></ul>Sale makeLineItem(desc,qty) quantity SalesLineItem 1..* lineItems class Sale { private SalesLineItem[ ] lineItems; public void makeLineItem(…) { } } using an array object to implement group of SalesLineItem s class Sale { private Vector lineItems; public void makeLineItem(…) { } } using a Vector object to implement group of SalesLineItem s
  14. 14. MESSAGES TO COLLECTIONS <ul><li>For example, consider the following: </li></ul>: Sale sli : SalesLineItem { new } 2.1: SalesLineItem(pd,qty) 2.2: add(sli) 2: makeLineItem(pd,qty) : ArrayList<SalesLineItem>
  15. 15. <ul><li>If the group of SalesLineItem objects is implemented using an array, </li></ul>class Sale { private SalesLineItem[ ] lineItems; private int numLineItems; public void makeLineItem(ProductDescription desc, int qty) { lineItems[numLineItems] = new SalesLineItem(desc, qty); numLineItems++; } }
  16. 16. <ul><li>If the group of SalesLineItem objects is implemented using a Vector object, </li></ul>class Sale { private Vector lineItems; public void makeLineItem(ProductDescription desc, int qty) { lineItems.addElement(new SalesLineItem(desc, qty)); } }
  17. 17. CHANGES DURING IMPLEMENTATION <ul><li>Remember! Expect and plan for changes and deviations from the design during programming. </li></ul><ul><li>Realistically, the results generated during design modelling are an incomplete first step; during programming and testing, lots of changes will be made and detailed problems will be uncovered and resolved. </li></ul>

×