Java Reference
In-Depth Information
4.3.1. Example: Vehicle Tracker Using Delegation
As a more substantial example of delegation, let's construct a version of the vehicle tracker
that delegates to a thread-safe class. We store the locations in a
Map
, so we start with a thread-
safe
Map
implementation,
ConcurrentHashMap
. We also store the location using an im-
Listing 4.6. Immutable
Point
class used by
DelegatingVehicleTracker
.
Point
is thread-safe because it is immutable. Immutable values can be freely shared and
published, so we no longer need to copy the locations when returning them.