OO Implementation
1. Introduction
With the completion of interaction diagrams and DCDs, there is sufficient detail to generate code for
the domain layer of objects. The UML artifacts created during the design work—the interaction
diagrams and DCDs—will be used as input to the code generation process.
The Implementation Model in UP contains the implementation artifacts such as the source code,
database definition, JSP/XML/HTML pages, and so forth.
source code and automatically generate, for example, package, class, and sequence diagrams. This is
an aspect of reverse-engineering—the activity of generating diagrams from source (or sometimes,
executable) code.
At the very least DCDs depict the class or interface name, superclasses, method signatures, and
simple attributes of a class. This is sufficient to create a basic class definition in an Object-oriented
programming language. The detail discussion will explore the addition of interface and namespace
(or package) information, among other details.
From the DCD, a mapping to the basic attribute definitions (simple Java instance fields) and method
signatures for the Java definition of SalesLineltem is straightforward, as shown in the figure below: -
ProductSpecification
SalesLineItem Described by desc : Text
quantity : Integer price : Money
+ProdSpec id : ItemID
getSubTotal:Money()
...()
Note the addition in the source code of the Java constructor SalesLineltem(). It is derived from the
create (spec, qty) message sent to a SalesLineltem 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.
A reference attribute is an attribute that refers to another complex object, not to a primitive type such
as a String, Number, and soon. The reference attributes of a class are suggested by the associations
and navigability in a class diagram.
Sales
date ProductSpecification
time SalesLineItem desc : Text
Contains +prodSpec
isComplete quantity : Integer price : Money
id : ItemID
1 n 1..n Described by 1
getTotal() getSubTotal()
makePayment() ...()
makeLineItem()
The Sale class illustrates that in some cases one must consider the mapping of attributes from the
design to the code in different languages. The figure below illustrates the problem and its resolution.
Sales
date ProductSpecification
time SalesLineItem desc : Text
Contains +prodSpec
isComplete quantity : Integer price : Money
id : ItemID
1 n 1..n Described by 1
getTotal() getSubTotal()
makePayment() ...()
makeLineItem()
An interaction diagram shows the messages that are sent in response to a method invocation. The
sequence of these messages translates to a series of statements in the method definition. The
enterItem interaction diagram illustrates the Java definition of the enterItem method.
Register ProductCatalog ProductSpecification Sale SalesLineItem
spec:=getSpec(id)
enterItem(id,qty) spec:=find(id)
makeLineItem(spec,qty) create(spec,qty)
sl:
SalesLineItem
add(sl)
Message 2:
s.makeLineItem(spec, qty)
In summary, each sequenced message within a method, as shown on the interaction diagram, is
mapped to a statement in the Java method.
9. Order of Implementation
Classes need to be implemented (and ideally, fully unit tested) from least-coupled to most-coupled.
As an example, a popular, simple and free unit testing framework is JUnit (www.junit.org) for Java.
Suppose we are using JUnit and test -first programming to create the Sale class. Before programming
the Sale class, we write a unit testing method in a SaleTest class that does the following:
1. Setup a new sale.
2. Add some line items to it.
3. Ask for the total, and verify it is the expected value.
For example:
public class saleTest extends Testcase
{
{
// …
public void testTotal()
// set up the test
Money total = new Money( 7.5 );
Money price = new Money( 2.5);
ItemID Id = new ItemID( 1 );
ProductSpecification spec;
spec = new Productspecification( id, price, product 1” );
Sale sale = new Sale();
// add the items
sale.makeLineItem( spec, 1);
sale.makeLinelten( spec, 2);
// verify the total is 7.5
assertEquals( sale.getTotal(), total);
}
}
Only after this SaleTest class is created do we then write the Sale class to pass this test. However, not
all unit testing methods need to be written beforehand. A developer writes one testing method, then
the production code to satisfy it, then another testing method, and soon.
Source: -
? Craig Larman, Applying UML and Patterns