Intent/Parcable/Bundle/SharedPreferences 的锅炉代码较少的 DTO 可能吗?

DTO-s with less boiler-code for Intent/Parcable/Bundle/SharedPreferences possible?

我有一个使用 DTO class MyAppData 的应用,其中有 21 个属性

我不得不编写大量锅炉代码来实现此功能。

我的问题:是否有更简单的方法和更少的锅炉代码来实现我的目标?

为什么是这个锅炉代码?

锅炉代码如下所示

    public class MyAppData implements Parcelable {

/****** here is the data that i want to use ******************/

        private String path = null;
        // ... 20 more attibutes

        public String getPath() {return path;}
        public void setPath(String path) {this.path = path;}
        // ... 20 more attibutes

/****** here start a lot of boiler-code necessary to handle the data ******************/

        /********** code needed to implement interface Parcelable *************/
        public static final Parcelable.Creator<MyAppData> CREATOR
                = new Parcelable.Creator<MyAppData>() {
            public MyAppData createFromParcel(Parcel in) {
                return new MyAppData(in);
            }

            public MyAppData[] newArray(int size) {
                return new MyAppData[size];
            }
        };

        public MyAppData() {};
        private MyAppData(Parcel in) {
            setPath(in.readString());
            // ... 20 more attibutes
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(getPath());
            // ... 20 more attibutes
        }

        @Override
        public int describeContents() {return 0;}

        /************ code neccessary to handle SharedPreferences(.Editor) because SharedPreferences cannot handle Parcable ********/

         private static final String SHARED_KEY_Path         = "filter_Path";
            // ... 20 more attibutes

        public void saveSettings(SharedPreferences.Editor edit) {

            if (edit != null) {
                edit.putString(SHARED_KEY_Path, this.getPath());
                // ... 20 more attibutes
            }
        }

        public void loadSettings(SharedPreferences sharedPref) {
            if (sharedPref != null) {
                this.setPath(sharedPref.getString(SHARED_KEY_Path, this.getPath()));
                // ... 20 more attibutes
            }
        }
    }

这是使用 MyAppData 及其锅炉代码的代码

MyAppData mMyData;

// to survive recreate on rotation
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    this.mMyData.saveInstanceState(this, savedInstanceState);
    super.onSaveInstanceState(savedInstanceState);
}

// to survive recreate on rotation or remember settings on last app start
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    this.mMyData.loadSettingsAndInstanceState(this, savedInstanceState);

}

@Override
protected void onPause () {
    ...
    this.mMyData.saveSettings(this);
}

// to pass to some other activity
private void openSomeActivity() {
    final Intent intent = new Intent().setClass(context,
            SomeActivity.class);

    intent.putExtra(EXTRA_FILTER, filter);

    context.startActivityForResult(intent, 0815);
}

// to receive from some other activity
@Override
protected void onActivityResult(final int requestCode,
                                final int resultCode, final Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
        case 0815 :
            myData = intent.getParcelableExtra(EXTRA_FILTER);
            break;
    }
}

我已经通过实现 dto 的字符串表示形式解决了我的问题:使用 toString()parse() 方法在 dto 和字符串之间进行转换。

这边

  • 通过 Intent 在活动之间传输,
  • saved/restored 在旋转的情况下通过 Bundle 和
  • 如果稍后重新启动应用程序,则通过 SharedPreferences(.Editor) 保留

全部通过字符串处理