Composite (Chapter 5)
SOLUTION 5.1 | One answer is that Design Patterns (Gamma et al. 1995) shows Component as an abstract class, so Java developers may implement it that way without considering the use of a Java interface. Another answer is that the Component superclass often has fields and methods that its subclasses can share. For example, the Component class may have a name instance variable and a concrete toString() method that uses it. |
SOLUTION 5.2 | For the Machine class, getMachineCount() should be something like:
public int getMachineCount() { return 1; } The class diagram shows that MachineComposite uses a List object to track its components. To count the machines in a composite, you might write: public int getMachineCount() { int count ... |
Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.