Sie sind auf Seite 1von 13



Implementation in an iteration
influences later design
Mapping Designs to Code
• Implementation in an object-oriented
programming language requires writing
source code for:
– class and interface definitions
– method definitions

Defining a Class with Methods and

Simple Attributes
• Note the addition in the source code of the Java
constructor SalesLineItem(...).
• It is derived from the create(spec, qty) message sent to
a SalesLineItem in the enterItem interaction diagram.
• This indicates, in Java, that a constructor supporting
these parameters is required.

• The create method is often excluded from the class

diagram because of its commonality and multiple
interpretations, depending on the target language.

Adding Reference Attributes

• A reference attribute is an attribute that
refers to another complex object, not to a
primitive type such as a String, Number, and
so on.
• The reference attributes of a class are
suggested by the associations and navigability
in a class diagram.
• A SalesLineItem has an association to a
ProductSpecification, with navigability to it.
• It is common to interpret this as a reference
attribute in class SalesLineItem that refers to a
ProductSpecification instance.

• In Java, this means that an instance field

referring to a ProductSpecification instance is

Adding Reference Attributes Example

Reference Attributes and Role Names
• The next iteration will explore the concept of
role names in static structure diagrams.
• Each end of an association is called a role.

• If a role name is present in a class diagram,

use it as the basis for the name of the
reference attribute during code generation,

Reference Attributes and Role Names

Mapping Attributes

Creating Methods from Interaction

• An interaction diagram shows the messages
that are sent in response to a method
• The sequence of these messages translates to
a series of statements in the method
Creating Methods from Interaction Diagrams:
The enterltem interaction diagram

Creating Methods from Interaction

Diagrams: The Register-enterltem Method
Container/Collection Classes in Code
• It is often necessary for an object to maintain
visibility to a group of other objects;
• The need for this is usually evident from the
multiplicity value in a class diagram—it may be
greater than one.

• In OO programming languages, these relationships are

often implemented with the introduction of a
intermediate container or collection. The one-side class
defines a reference attribute pointing to a
container/collection instance, which contains instances of
the many-side class.

Sale must maintain visibility

to a group of SalesLineltem instances
Defining the Sale--makeLineltem

Possible order of class

implementation and testing
Program Solution
• Sample domain object layer program solution
in Java for this iteration:

– The code generation is largely derived from the

• design class diagrams
• and interaction diagrams
defined in the design work.

Class Payment
public class Payment {
private Money amount;
public Payment( Money cashTendered ){ amount = cashTendered; }
public Money getAmount() { return amount; } }
Class ProductCatalog
public class ProductCatalog {
private Map productSpecifications = new HashMap();
public ProductCatalog() {
// sample data
ItemID idl = new ItemID( 100 );
ItemID id2 = new ItemID( 200 );
Money price = new Money( 3 );
ProductSpecification ps;
ps = new ProductSpecification( idl, price, "product 1" );
productSpecifications.put( idl, ps );

ps = new ProductSpecification( id2, price, "product 2" );

ProductSpecifications.put( id2, ps ); }
public ProductSpecification getSpecification( ItemID id ) {
return (ProductSpecification)productSpecifications.get( id );

Class Register
public class Register {
private ProductCatalog catalog;
private Sale sale;
public Register( ProductCatalog catalog ) {
this.catalog = catalog; }
public void endSaleO {
public void enterltem( ItemID id, int quantity ) {
ProductSpecification spec = catalog.getSpecification( id );
sale.makeLineItem( spec, quantity ); }
public void makeNewSale() {
sale = new Sale(); }
public void makePayment( Money cashTendered ) {
sale.makePayment( cashTendered ); }
Class ProductSpecification
public class ProductSpecification {
private ItemID id;
private Money price;
private String description;
public ProductSpecification
( ItemID id. Money price. String description ) { = id;
this.price = price;
this.description = description; }
public ItemID getltemlDO { return id;}
public Money getPrice() { return price; }
public String getDescription() { return description; }

Class Sale
public class Sale
private List lineltems = new ArrayListO;
private Date date = new Date();
private boolean isComplete = false;
private Payment payment;
public Money getBalanceO {
return payment.getAmount().minus( getTotal() ); }
public void becomeComplete() { isComplete = true; }
public boolean isComplete() { return isComplete; }
public void makeLineltem
( ProductSpecification spec, int quantity ) {
lineltems.add( new SalesLineltem( spec, quantity ) ); }
public Money getTotal()
Money total = new MoneyO;
Iterator i = lineltems.iterator( ) ;
while ( i.hasNextO )
SalesLineltem sli = (SalesLineltem) i.nextO;
total.add( sli.getSubtotal() );
return total; }
public void makePayment( Money cashTendered )
payment = new Payment( cashTendered ); } }
Class SalesLineltem
public class SalesLineltem {
private int quantity;
private ProductSpecification productSpec;
public SalesLineltem (ProductSpecification spec, int
quantity )
this.productSpec = spec;
this.quantity = quantity; }
public Money getSubtotal() {
return productSpec.getPrice().times( quantity );

Class Store

public class Store

private ProductCatalog catalog = new ProductCatalog();
private Register register = new Register( catalog );
public Register getRegister() { return register; } }