The path to code cleanliness
The Map ing code of the Sensor class object is as follows:
Map sensors = new HashMap ();
When other parts of the code access these sensors, the following code appears:
Sensors s = (Sensors) sensors. get (sensorId );
This line of code appears again and again. The Code caller is responsible for obtaining objects from the Map and converting them to the correct type. The line is a line, but not a neat code. In addition, this line of code does not describe its own use.
Through the use of generics, the code readability is greatly improved, as shown in the following example:
Map <Sensors> sensors = new HashMap <Sensors> ();
String s = sensors. get (sensorId );
However, the problem of Map <Sensors> exceeding the required functions has not been solved yet.
In the system, the Map <Sensors> object is passed without restriction, which means that when the interface to Map is modified, many parts must be modified.
The more concise method of using Map is as follows. Sensors does not have to worry about whether generics are used, so it will be concerned about implementation details.
Public class Sensors {private Map sensors = new HashMap (); public Sensor getById (String id) {return (Sensor) sensors. get (id );}}
The interface (Map) on the border is hidden. It can change with minimal impact from other parts of the application.
Error Message
Error Message