As we continue examining
the major design principles of object oriented modeling and programming,
you're now going to learn about decomposition.
I'll explain how decomposition works on its own.
But, it's important to keep in mind the design principles work well together.
Decomposition is taking a whole thing and dividing it up into different parts.
Or, on the flip side taking a bunch of separate parts with different functionalities,
and combining them together to form a whole.
Decomposition allows you to further break down
problems into pieces that are easier to understand and solve.
Let's consider a whole thing,
like a car or a refrigerator.
By breaking down such a thing into its different parts using decomposition,
you can more easily keep their different responsibilities separate.
A general rule for decomposition is to look at
the different responsibilities of some whole thing,
and evaluate how you can separate them into different parts,
each with its own specific responsibility.
This relates one whole to multiple different parts.
Let's get some practice breaking down a whole thing into its different constituent parts.
Identify the different constituent parts of a car.
Some possible parts of your car maybe a transmission,
a motor, wheels, tires,
doors, windows, seats, and fuel.
For another example, a refrigerator also has several parts.
There is the cabinet and doors,
compressor and coils, freezer,
ice-maker, shelves, drawers, and of course food if the refrigerator is stocked.
Each of those parts has
a very specific purpose to help achieve the responsibilities of the whole.
For refrigerator, the parts work together to achieve the overall purpose of keeping,
and preserving food in cold storage.
Sometimes the whole will delegate specific responsibilities to the parts.
So, the refrigerator delegates the freezing of
food and the storing of that food to the freezer.
Since decomposition allows you to create clearly defined parts,
it is quite natural that these parts are separate.
Let's see how parts interact within the whole.
A whole might have a fixed or dynamic number of a certain type of part.
If there is a fixed number,
then over the lifetime of the whole object it
will have exactly that much of the part object.
For example, a refrigerator has a fixed number of freezers, just one.
This does not change over time,
but there are sometimes parts with a dynamic number.
Meaning, the whole object may gain new instances of those part objects over its lifetime.
For example, a refrigerator can have a dynamic number of shelves or food items over time.
Now, for some practice in determining whether you
have a fixed or dynamic number of a part.
Fixed parts don't change in number over time.
So by a process of elimination,
we can find out which car parts are dynamic.
We know a typical car has one steering wheel,
four tires, and one engine at any given time.
These numbers do not fluctuate.
The number of passengers can change though, making it dynamic.
A part itself can also serve as a whole containing further constituent parts.
When you look at our refrigerator example,
a drawer can contain fruit.
Let's come up with another example of one part containing other parts.
Thinking of a car, can you describe an example of
decomposition where one part contains another?
You may have come up with several different examples.
The one that comes to mind for me is the instrument panel,
which contains a fuel gauge,
an odometer, and a speedometer among other parts.
Also, because of encapsulation,
the instrument panel treats the instruments like black boxes,
and doesn't care about how they are implemented.
One issue in decomposition involves the lifetimes of the whole object,
and the part objects, and how they could relate.
Lifetimes might be closely related.
For example, the refrigerator and its freezer have the same lifetime.
One cannot exist by itself without the other.
If you dispose off the refrigerator,
you would dispose off the freezer as well.
But lifetime can also not be so related.
The refrigerator and food items have different lifetimes.
Either can exist independently.
Let's take a look at the lifetimes of parts in an example.
Consider the lifetime of a car,
and name one part of a car that has a closely related lifetime,
and one part that is not.
As possible responses, a closely related lifetime would be the frame,
and not closely related would be the tires.
You can have whole things contain parts that are shared among them at the same time.
How can this relationship arise?
Consider a person who has a daughter in one family,
but also a spouse in another family.
The two families are regarded as separate wholes,
but they simultaneously share the same part.
However, sometimes sharing is not possible or intended.
For example, a food item in a refrigerator
cannot at the same time also be inside an oven.
Overall, decomposition helps you to break down a problem into smaller pieces.
A complicated whole thing can be composed out of constituent, separate, simpler parts.
Important issues to understand are how the parts relate to the whole,
such as fixed or dynamic number,
their lifetimes, and whether there is sharing.