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

Maping Design To Code

1) The document describes how to map a design represented using interaction diagrams and domain class diagrams to code. This involves creating class definitions, method definitions, and using collections to represent relationships between classes. 2) Key classes like Payment, ProductDescription, and ProductCatalog can be implemented first as they have few dependencies. Then classes like SalesLineItem and Sale can be implemented as they depend on the prior classes. 3) Collections like List are used to represent one-to-many relationships between classes like the line items in a sale.

Uploaded by

Bhupendra Bhat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Maping Design To Code

1) The document describes how to map a design represented using interaction diagrams and domain class diagrams to code. This involves creating class definitions, method definitions, and using collections to represent relationships between classes. 2) Key classes like Payment, ProductDescription, and ProductCatalog can be implemented first as they have few dependencies. Then classes like SalesLineItem and Sale can be implemented as they depend on the prior classes. 3) Collections like List are used to represent one-to-many relationships between classes like the line items in a sale.

Uploaded by

Bhupendra Bhat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Mapping Designs to Code

Mapping Design to code

With the completion of interaction diagram


& DCD,
One can start generation of code where in
the interaction diagram & DCD can be used
as the input.
Implementation of OO language requires
writing source code for:
Class & interface definitions
Method definitions
Creating class
definitions from DCDs
At the very least, DCDs depict the class or interface
name, superclasses, operation signatures, and
attributes of a class.
This is sufficient to create a basic class definition in
an OO language.
If the DCD was drawn in a UML tool, it can generate
the basic class definition from the diagrams.
Creating Class Definitions from DCDs
public class SalesLineItem
{
private int quantity ;

private ProductDescription description ;

public SalesLineItem(ProductDescription desc , int qty)

{ ... } public Money getSubtotal () { ... }

ProductDescription
SalesLineItem
description description : Text
quantity : Integer price : Money
1 itemID : ItemID
getSubtotal() : Money
...
Creating Methods from Interaction
Diagrams (Register.enterItem)

{
ProductDescription desc= catalog.ProductDescription(id); enterItem
currentSale.makeLineItem(desc, qty);
} method
of
Collections
• One-to-many relationships are common.
– For example, a Sale must maintain visibility to a group of many
SalesLineItem instances.
– In OO programming languages, these relationships are usually
implemented with the introduction of a collection object, such as a
List or Map, or even a simple array.
• Java libraries contain collection classes such as ArrayList and
HashMap, which implement the List and Map interfaces,
respectively.
• The choice of collection class is influenced by the
requirements;
– key-based lookup requires the use of a Map,
– a growing ordered list requires a List, and so on.
Collections

Sale
public class Sale
{ isComplete :
... Boolean time : SalesLineItem
DateTime
private List lineItems= new ArrayList(); e lineItems
becomeComplet () quantity : Integer
} 1..*
makeLineItem() getSubtotal()
makePayment()
getTtotal()

A collection class is necessary to


maintain attribute visibility to all the
SalesLineItems.
Order of Implementation
• Classes need to be implemented from least-coupled to most-
coupled.
• E.g.,
– possible first classes to implement are either Payment or
ProductDescription;
– next are classes only dependent on the prior implementations—
ProductCatalog or SalesLineItem.
Order of Implementation
Store
7
address : Address
1
name : Text ProductDescription
2
ProductCatalog
3
addSale(...) description : Text
... price : Money
1 itemID : ItemID
1..*
getProductDes c(...)
...

1
1 Sale 5
6
Register isComplete : 4
SalesLineItem
Boolean time :
...
DateTime
becomeComplet e() quantity : Integer
endSale() 1 1..*
makeLineItem(...)
enterItem(...) getSubtotal(
makePayme n(...)
makeNewSale() )
getTotal() t
makePayme n(...)
...
t
1
* Payment

amount :
1 Money
...
How to design makeNewSale ?
by Creator
and
Controller Register creates a
Sale by Creator by Creator, Sale
:Register creates an empty
collection (such as a
List) which will
eventually hold
SalesLineItem
instances
makeNewSale
create
:Sale

create lineItems :
List<SalesLineItem>

this execution specification is implied to be


within the constructor of the Sale instance
enterItem
getTotal
makePayment, getBalance
endSale
Class Payment
// all classes are probably in a package named
// something like:
package com.foo.nextgen.domain;

public class Payment


{
private Money amount;

public Payment( Money cashTendered ){


amount = cashTendered;
}
public Money getAmount() {
return amount;
}
}
Class ProductDescription
public class ProductDescription
{
private ItemID id;
private Money price;
private String
description;

public
ProductDescription
( ItemID id,
Money price,
String
description )
{
this.id = id;
this.price = price;
this.description =
description;
}

public ItemID getItemID() { return id;


Class ProductCatalog
public class ProductCatalog
{
private Map<ItemID, ProductDescription>
descriptions = new HashMap()<ItemID, ProductDescription>;

public ProductCatalog() {
// sample data
ItemID id1 = new ItemID( 100 );
ItemID id2 = new
ItemID( 200 ); Money price =
new Money( 3 );

ProductDescription desc;
desc = new ProductDescription( id1, price, "product 1" );
descriptions.put( id1, desc );
desc = new ProductDescription( id2, price, "product 2" );
descriptions.put( id2, desc );
}

public ProductDescription getProductDescription( ItemID id ) {


return descriptions.get( id );
}
Class SalesLineItem
public class SalesLineItem
{
private ProductDescription description;
private int quantity;

public SalesLineItem
(ProductDescription desc, int quantity
)
{
this.description = desc;
this.quantity = quantity;
}

public Money getSubtotal()


{
return
description.getPrice().ti
mes( quantity );
}
}
Class Sale
public class Sale {
private List<SalesLineItem> lineItems =
new ArrayList()<SalesLineItem>;
private Date date = new Date();
private
boolean isComplete = false;
private Payment payment;

public Money
getBalance()
{
return
payment.getAmount().minus( getTo
tal() );
}

public void becomeComplete()

{ isComplete = true; } public boolean isComplete()

{ return isComplete; }
Class Sale
public Money getTotal()
{
Money total = new Money();
Money subtotal = null;

for ( SalesLineItem
lineItem : lineItems )
{
subtotal = lineItem.getSubtotal();
total.add( subtotal );
}
return total;
}

public void makePayment( Money


cashTendered )
{
payment = new
Payment( cashTendered );
}
} //end of sale
Class Register
public class Register {
private ProductCatalog catalog;
private Sale currentSale;

public Register( ProductCatalog catalog ) {


this.catalog = catalog;
}

public void makeNewSale() { currentSale = new Sale(); }

public void enterItem( ItemID id, int quantity ) {


ProductDescription desc = catalog.getProductDescription(id);
currentSale.makeLineItem( desc, quantity );
}

public void makePayment( Money cashTendered ) {


currentSale.makePayment( cashTendered );
}

public void endSale()


{ currentSale.becomeComplete(); }
Class Store
public class Store
{
private ProductCatalog catalog = new ProductCatalog();
private Register register = new Register( catalog );

public Register getRegister() { return register; }


}
Mapping design to code :example

Working Example: PM:

PM: Use Case Diagram


PM: Class Diagram

PM: Class to Code: they are coded as:


- class WorkPackage;
- class Project;
- class Activity;
- class Task;
- class WorkProduct;
- class Resource;
- class Skill;
- class ResourceXSkill;

class WorkPackage
{ // Details omitted };
class Project : public WorkPackage

{ private: CollectionByVal<Activity> theActivity; };


class Activity : public WorkPackage
{ private: Project *theProject;
CollectionByVal<Task> theTask;
CollectionByRef<WorkProduct>
t
heWorkPro
duct; };
PM: DCD Mapping

PM: DCD Code:


class Project
{ private:
char *Name;
char *Descr;
Date StartDate;

static int NumberOfProjects;


public:
Project (char *Name);

Project (void); ~Project


(void); char *getName (void);
void setName (char
*theName);

void setDescr (char


*Descr); char *getDescr
(void);
void setStartDate (Date
theStartDate)
; Date getStartDate (void);
void
addActivity (const Activity
&theActivity);
CollectionByRef<Activity> getAllAcitivities (void);
static int getNumberOfProjects (void);
void save (void);
void load (char *Name);
protected:
bool hasActivities
(void); };
int
Project::NumberOfProject
s = 0;

PM: Sequence Diagram

PM: Sequence to Main


void main (void)

{ char *Name; char *Descr; Date


StartDate; Project aProject;
// provide project Name, descr, and
startdate
aProject.setName (Name);
aProject.setDescr (Descr);
aProject.setStartDate (StartDate);
aProject.save (); }

You might also like