The path to code cleanliness

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.