- 20.1 Programming and Iterative, Evolutionary Development
- 20.2 Mapping Designs to Code
- 20.3 Creating Class Definitions from DCDs
- 20.4 Creating Methods from Interaction Diagrams
- 20.5 Collection Classes in Code
- 20.6 Exceptions and Error Handling
- 20.7 Defining the Sale.makeLineItem Method
- 20.8 Order of Implementation
- 20.9 Test-Driven or Test-First Development
- 20.10 Summary of Mapping Designs to Code
- 20.11 Introduction to the NextGen POS Program Solution
- 20.12 Introduction to the Monopoly Program Solution
20.4 Creating Methods from Interaction Diagrams
The sequence of the messages in an interaction diagram translates to a series of statements in the method definitions. The enterItem interaction diagram in Figure 20.2 illustrates the Java definition of the enterItem method. For this example, we will explore the implementation of the Register and its enterItem method. A Java definition of the Register class is shown in Figure 20.3.
Figure 20.2 The enterItem interaction diagram.
The enterItem message is sent to a Register instance; therefore, the enterItem method is defined in class Register.
public void enterItem(ItemID itemID, int qty)
Message 1: A getProductDescription message is sent to the ProductCatalog to retrieve a ProductDescription.
ProductDescription desc = catalog.getProductDescription(itemID);
Message 2: The makeLineItem message is sent to the Sale.
In summary, each sequenced message within a method, as shown on the interaction diagram, is mapped to a statement in the Java method.
The complete enterItem method and its relationship to the interaction diagram is shown in Figure 20.4.
The Register.enterItem Method
Figure 20.3 The Register class.
Figure 20.4 The enterItem method.