Start learning free

LESSON 319 OF 343 · DESIGN PATTERNS

Adapter Pattern in C#

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 interface ISensor has one method, DistanceMm(). It returns that distance in millimetres (mm). An interface lists the methods a class promises to have.

Write three classes:

  • new LaserSensor(mm) implements ISensor. Its Set(mm) changes the distance it measures. Its DistanceMm() returns the current one.
  • new SonarAdapter(device) implements ISensor 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 DistanceMm() reads the device again. It turns tenths of an inch into mm with tenths * 254 / 100, rounded down.
  • new Robot() keeps its sensors. Its Add(sensor) adds one. Its Closest() returns the smallest DistanceMm() 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:

  • new SonarDevice(tenths) makes a sonar that reads tenths.
  • Update(tenths) changes the reading.
  • ReadTenths() 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 DistanceMm().
  • 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 19
    Ctrl + Enter to check

    Adapter Pattern in C#

    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free