Understanding Decomposition
Decomposition
The process of breaking down a complex problem into smaller, more manageable parts.
In OOP, this involves identifying objects and their relationships to model a system effectively.
Analogy- Think of decomposition like assembling a puzzle.
- Each piece (object) fits into a larger picture (the system), and understanding each piece helps you solve the entire puzzle.
Some of real-world examples:
- Calendar application:
- Objects: Calendar, Year, Month, Day, Event
- Relationships: A Calendar contains multiple Years, each Year has Months, and so on.
- Music player:
- Objects: Track, Playlist, Player, User
- Relationships: A Playlist contains multiple Tracks, and a User interacts with the Player that plays a Track.
Now, let's see how we can decompose more complex systems.
Example scenario: Delivery System
Let's explore how to decompose a delivery system into objects.
Identify the main entities:
- Package: Represents the item being delivered.
- DeliveryPerson: Responsible for transporting the package.
- Vehicle: Used by the delivery person to transport packages.
- Destination: The final location where the package is delivered.
Define the attributes and methods:
| Object | Attributes | Methods |
|---|---|---|
| Package | id, weight, dimensions, status, sender | updateStatus(), track() |
| DeliveryPerson | name, id, currentLocation | pickUpPackage(), deliverPackage() |
| Vehicle | type, capacity, fuelLevel | loadPackage(), drive() |
| Destination | address, coordinates, recipient | confirmDelivery() |
Establish relationships:
- DeliveryPerson uses a Vehicle to transport a Package to the Destination.
- A Package has a Destination where it needs to be delivered.
