(Android Studio) 8.16 extract parameter objects

Source: Internet
Author: User

(Android Studio) 8.16 extract parameter objects

Some parameters always appear at the same time. Several methods may use such a set of parameters. To avoid too long parameter lists and avoid repeated code, we can extract these parameters into parameter objects, this parameter object is used to replace the original input parameter.

Procedure:

? Menu Bar: Refactor-> Extract-> Parameter Object...

Example:

Before extracting parameter objects:

public class TestExtract {    private void getAndroidInfo(String serialNumber, String packageName, String versionName, String versionCode){        DeviceInfo deviceInfo = new DeviceInfo();        deviceInfo.setSerialNumber(serialNumber);        PackageInfo packageInfo = new PackageInfo();        packageInfo.setPackageName(packageName);    }}

Step 2: select all parameters of the getAndroidInfo Parameter-> execution menu bar: Refactor-> Extract-> Parameter Object... -> The parameter configuration dialog box is displayed.

Step 2: In this example, create an internal class named AndroidInfo. All parameters are extracted by default-> Refactor

After the parameter object is extracted:

public class TestExtract {    private void getAndroidInfo(AndroidInfo androidInfo){        DeviceInfo deviceInfo = new DeviceInfo();        deviceInfo.setSerialNumber(androidInfo.getSerialNumber());        PackageInfo packageInfo = new PackageInfo();        packageInfo.setPackageName(androidInfo.getPackageName());    }    private static class AndroidInfo {        private final String serialNumber;        private final String packageName;        private final String versionName;        private final String versionCode;        private AndroidInfo(String serialNumber, String packageName, String versionName, String versionCode) {            this.serialNumber = serialNumber;            this.packageName = packageName;            this.versionName = versionName;            this.versionCode = versionCode;        }        public String getSerialNumber() {            return serialNumber;        }        public String getPackageName() {            return packageName;        }        public String getVersionName() {            return versionName;        }        public String getVersionCode() {            return versionCode;        }    }}

Related Article

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.