Start learning free

LESSON 319 OF 343 · DESIGN PATTERNS

Adapter Pattern

Practice Structural

Sometimes the class you need doesn't fit the code that should use it. Its methods have other names, or they work in other units. Often it comes from a library you can't change. The Adapter pattern puts a small class in between. It has the methods your code expects, and it passes each call on to the other class, translated.

Here you write the sensors of a robot. The robot asks each sensor how far the nearest obstacle is.

The base class Sensor has one method, distance_mm(). It returns that distance in millimetres (mm). It plays the part of an interface. Each sensor class is built on it and writes its own distance_mm().

Write three classes:

  • LaserSensor(mm) is built on Sensor. Its set(mm) changes the distance it measures. Its distance_mm() returns the current one.
  • SonarAdapter(device) is built on Sensor too. It wraps a SonarDevice from the library below. Wrapping means it keeps the device in a field and calls it. Each call of its distance_mm() reads the device again. It turns tenths of an inch into mm with tenths * 254 / 100, rounded down.
  • Robot() keeps its sensors. Its add(sensor) adds one. Its closest() returns the smallest distance_mm() of all its sensors. With no sensors, it returns -1.

The sonar's library is already in the code, and you must not change it. Its class SonarDevice has three methods:

  • SonarDevice(tenths) makes a sonar that reads tenths.
  • update(tenths) changes the reading.
  • read_tenths() returns the reading, in tenths of an inch.

The tests make the lasers and the sonar devices themselves. They wrap each device in a SonarAdapter and give every sensor to add. Later they change readings on their own objects. They call set on a laser, or update on a device. So the adapter must read its device at every call.

Each example runs commands on a new robot, one per line:

  • laser mm makes a laser that measures mm and adds it. It writes the sensor's number: 1 for the first, then 2, 3 and so on.
  • sonar tenths makes a device that reads tenths, wraps it in an adapter and adds it. It writes the sensor's number too.
  • set i value gives sensor i a new reading. For a laser, value is in mm. For a sonar, it is in tenths of an inch.
  • read i writes sensor i's distance_mm().
  • closest writes the robot's closest().

 

Example 1:

Input:
1. closest
2. laser 500
3. sonar 300
4. read 2
5. closest
6. set 2 150
7. read 2
8. closest
9. set 1 200
10. closest
Output:
-1
1
2
762
500
381
381
200
Explanation:
Command 1 finds no sensor, so it writes -1.
Sensor 1 is a laser at 500 mm. Sensor 2 is a sonar that reads 300 tenths.
That is 300 * 254 / 100 = 762 mm.
So the laser is the closest, at 500 mm.
Command 6 changes the reading of the sonar's device to 150 tenths.
The adapter reads the device again and gets 381 mm.
Now the sonar is the closest.
Command 9 moves the laser to 200 mm, so it is the closest again.

Example 2:

Input:
1. sonar 7
2. read 1
3. laser 18
4. closest
5. set 1 8
6. read 1
7. closest
Output:
1
17
2
17
20
18
Explanation:
The sonar reads 7 tenths, and 7 * 254 / 100 is 17.78.
Rounded down, that is 17 mm.
The laser at 18 mm is farther, so the closest is 17.
Command 5 sets the device to 8 tenths. That is 20.32 mm, rounded down to 20.
Now the laser at 18 mm is the closest.

 

Constraints:

  • mm is a whole number from 0 to 1,000,000.
  • tenths is a whole number from 0 to 1,000,000.
  • value is a whole number from 0 to 1,000,000.
  • A robot has at most 20 sensors.
  • i is always the number of a sensor already added.
  • There are at most 100,000 commands in all.

Note:

Each call should run in O(1) time. A robot has at most 20 sensors, so closest() is O(1) too. The largest tenths * 254 is 254,000,000, which fits in a 32-bit integer.

STEP 1 OF 18
    Ctrl + Enter to check