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.
- 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.
Example: Traffic Simulation Model
Let's apply decomposition to a traffic simulation model.
Identify the main entities:
- Vehicle: Represents cars, trucks, etc.
- TrafficLight: Controls traffic flow.
- Road: Connects different locations.
Define the attributes and methods:
| Object | Attributes | Methods |
|---|---|---|
| Vehicle | type, speed, position | move(), stop() |
| TrafficLight | state (red, yellow, green) | changeState() |
| Road | length, type | addVehicle(), removeVehicle() |
Establish Relationships:
- Vehicles move on Roads.
- TrafficLights control the flow of Vehicles at intersections.
One of possible systems may look like:
- Decomposition is not a one-time process.
- As you refine your understanding of a problem, you may need to revisit and adjust your decomposition.
How does the process of decomposition reflect human problem-solving strategies in other fields, such as engineering or medicine?