Skip to main content

Create classes from design class diagrams

Short answer

Design class diagrams map to classes: attributes become fields with visibility, operations become methods, associations become references or collections, and interaction diagrams fill method bodies. For example, Sale.makeLineItem() creates SalesLineItem, and getTotal() delegates to subtotal calculations.

Illustrate how can you create classes from design class diagrams and methods from interaction diagrams (Use C#, Java etc.). (With the help of suitable object oriented implementation code, describe the procedure/process of creating class definitions from Design Class Diagrams.)

A design class diagram (DCD) shows software classes with attributes (with types and visibility), method signatures, and associations with navigability and multiplicity. It is converted to code class by class using these rules.

Rules of mapping (Java)

  1. Each class becomes a class definition. An abstract class uses abstract, and an interface becomes interface.
  2. Each attribute becomes a field with the stated visibility (- private, + public, # protected) and type.
  3. Each operation becomes a method with the given signature. The body is filled in using the messages of the interaction diagram (sequence or collaboration).
  4. Generalization becomes extends; realization becomes implements.
  5. An association with navigability becomes a reference attribute in the source class. Multiplicity 1 gives a field, and 0..* or 1..* gives a collection (List<T>).
  6. Composition: the whole creates its parts (in the constructor or a factory method).
  7. Add constructors, getters and setters.
 +---------------+ 1     1..* +-----------------+
 |     Sale      |<>----------| SalesLineItem   |
 |---------------|            |-----------------|
 | -date: Date   |            | -quantity: int  |
 |---------------|            |-----------------|
 | +getTotal():  |            | +getSubtotal()  |
 |   double      |            |   : double      |
 | +makeLineItem |            +-----------------+
 |  (spec, qty)  |
 +---------------+

SalesLineItem also has a * to 1 association to ProductSpecification (with getPrice()), which is needed to find the price. It is not drawn above, but it appears in the code.

Code for the DCD

import java.util.*;

class ProductSpecification {
    private String itemId;
    private double price;
    public ProductSpecification(String id, double price) { itemId = id; this.price = price; }
    public double getPrice() { return price; }
}

class SalesLineItem {
    private int quantity;
    private ProductSpecification spec;           // association *-1
    public SalesLineItem(ProductSpecification spec, int qty) {
        this.spec = spec; this.quantity = qty;
    }
    public double getSubtotal() { return quantity * spec.getPrice(); }
}

class Sale {
    private Date date = new Date();
    private List<SalesLineItem> lineItems = new ArrayList<>(); // 1..*
    public void makeLineItem(ProductSpecification spec, int qty) {
        lineItems.add(new SalesLineItem(spec, qty));   // Creator
    }
    public double getTotal() {
        double total = 0;
        for (SalesLineItem li : lineItems) total += li.getSubtotal();
        return total;
    }
}

Creating methods from an interaction diagram

In the sequence diagram, getTotal() is sent to :Sale, which sends getSubtotal() to each :SalesLineItem, which sends getPrice() to :ProductSpecification. This gives exactly the method bodies above: each message sent in the diagram becomes a method call inside the method of the sender. The message makeLineItem(spec, qty) to :Sale, followed by create(spec, qty) to SalesLineItem, becomes makeLineItem() using new SalesLineItem(...).

✓ Answer checked by Claude Sonnet 5.5

All Object Oriented Analysis and Design chapter 4 questions →