CO2401: Software Development
Lecture 15
Structural Patterns
1. Composite Pattern
o Problem:
Used when an application’s model can be represented as a
tree structure, such as an order containing products packed
in boxes within boxes.
Challenge: Calculating the total price of such an order.
o Solution:
Use a common interface for both products and boxes, with a
method to calculate the total price.
For a product: Return its price.
For a box: Iterate over its contents, summing up prices,
including any additional costs like packaging.
2. When to Use:
o Implement a tree-like structure in your object model.
o Enable client code to treat simple and complex elements uniformly.
3. Pros and Cons:
o Benefits:
Simplifies working with complex tree structures.
Allows new element types to be added without breaking
existing code.
o Drawbacks:
Difficulty in providing a common interface for classes with
significantly different functionalities.
Adapter Pattern
Lecture 15
Structural Patterns
1. Composite Pattern
o Problem:
Used when an application’s model can be represented as a
tree structure, such as an order containing products packed
in boxes within boxes.
Challenge: Calculating the total price of such an order.
o Solution:
Use a common interface for both products and boxes, with a
method to calculate the total price.
For a product: Return its price.
For a box: Iterate over its contents, summing up prices,
including any additional costs like packaging.
2. When to Use:
o Implement a tree-like structure in your object model.
o Enable client code to treat simple and complex elements uniformly.
3. Pros and Cons:
o Benefits:
Simplifies working with complex tree structures.
Allows new element types to be added without breaking
existing code.
o Drawbacks:
Difficulty in providing a common interface for classes with
significantly different functionalities.
Adapter Pattern