March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class Hull { |
| | int holes; |
| | } |
| | |
| | |
| | class HullRepairUnit { |
| | |
| » | void repairHole(Hull hull) { |
| | if (isIntact(hull)) { |
| | return; |
| | } |
| | hull.holes--; |
| | } |
| | |
| » | boolean isIntact(Hull hull) { |
| | return hull.holes == 0; |
| | } |
| | } |
The combination of state and behavior is one of the cornerstones of object-oriented programming. Classes containing only behavior but lacking state indicate OO-design problems.
In the code snippet here, the Hull class captures state and tracks the amount of its holes. The HullRepairUnit can fix those holes, and it captures behavior.
The code above separates state and behavior into two distinct classes. This separation is something you’ll find quite often in a beginner’s code. Generic examples ...