- 2070 Bhadra · 8 marks
Order total and subtotal() redesign
Short answer
The original Order.total() loops over OrderLines, calling getArticle().getPrice() and getQuantity() per line, causing 3n messages and violating Law of Demeter. Adding subtotal() to OrderLine lets it compute price*quantity locally, reducing Order to n messages (subtotal) plus one getPrice per line, cutting messages from 3n to 2n, reducing coupling, and enabling caching.
Your manager reviews your design and codes of a sub-module that you have prepared, as depicted in the below figure. She suggests you to introduce subtotal( ) somewhere in your model thinking about the performance of the system. [Figure: class Order with method total(), with a note attached: "for all my OrderLines ol, tot = tot + ol.getArticle().getPrice() * ol.getQuantity()"; Order is linked to class OrderLine with methods getQuantity() and getArticle(); OrderLine is linked to class Article with method getPrice().] Modify your implementation and justify how does this update provides you better performance.
Present design and its problem
Order.total() loops over all its OrderLines. For each line it calls ol.getArticle(), then article.getPrice(), and ol.getQuantity(), and then multiplies:
for each ol in orderLines:
tot = tot + ol.getArticle().getPrice() * ol.getQuantity()
For n lines this makes 3n messages from Order. Order also has to know about Article: it depends on two other classes and reaches through OrderLine to Article, which violates the Law of Demeter. If the objects are remote or loaded from a database, each call may be a network round trip or a query.
Modified design: add subtotal() to OrderLine
OrderLine already knows its quantity and its article, so by the Information Expert principle it should compute its own subtotal.
+---------+ 1 * +-----------+ * 1 +---------+
| Order |-------| OrderLine |-------| Article |
|---------| |-----------| |---------|
| total() | | getQuantity() | getPrice()
+---------+ | getArticle() +---------+
| subtotal() <- new
+-----------+
class OrderLine {
private int quantity;
private Article article;
public double subtotal() { // new method
return article.getPrice() * quantity;
}
}
class Order {
private List<OrderLine> lines = new ArrayList<>();
public double total() {
double tot = 0;
for (OrderLine ol : lines)
tot += ol.subtotal(); // one message per line
return tot;
}
}
Why performance improves
- Fewer messages:
Ordersends n messages (subtotal) instead of 3n. The quantity is now read directly insideOrderLine, and only one message toArticle(getPrice) remains per line, so the total falls from 3n to 2n messages. - Fewer round trips in a distributed or persistent system:
Ordermakes one call per line instead of three, so there is less network and database traffic and less object loading. - Lower coupling:
Orderno longer depends onArticle; it needs to know onlyOrderLine. A change in the price calculation (discount, tax) is made only insubtotal(). - Easier caching and optimisation:
OrderLinecan cache its subtotal, andOrdercan cache the total until a line changes. Each class manages its own data. - Reuse: the subtotal can be used by invoices and reports without repeating the formula.
In a single-process program the saving is small. The real gain is in distributed or database-backed systems, together with better cohesion and maintainability.
✓ Answer checked by Claude Sonnet 5.5
Related questions
- Event management system summary and class diagram · Object Oriented Analysis and Design, chapter 2
- Create classes from design class diagrams · Object Oriented Analysis and Design, chapter 4 · asked 2×
- KUKL customer system design and code · Object Oriented Analysis and Design, chapter 4
- Constraint specifications in bank account model · Object Oriented Analysis and Design, chapter 2
- XYZ Marina class diagram · Object Oriented Analysis and Design, chapter 2
- Four class relationships from figure · Object Oriented Analysis and Design, chapter 2
- Canal outlet and module performance criteria · Irrigation and Drainage Engineering, chapter 7
- Adjustable orifice semi-module design · Irrigation and Drainage Engineering, chapter 7
- Second order model from step response (peak 1.25) · Control Systems, chapter 3
- Design goals and performance metrics · Computer Organization and Architecture, chapter 1
All Object Oriented Analysis and Design chapter 3 questions →