I have two methods which are nearly the same, how to refactor them?

别说谁变了你拦得住时间么 提交于 2020-01-25 04:38:53

问题


I am having two DatePickerFragments because I need to pick up a start and end time.

private void showDatePickerTimePeriodStart() {
    final DatePickerFragment date = new DatePickerFragment();
    // Sets up the current date in Dialog.
    final Calendar calender = Calendar.getInstance();
    final Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    // Sets callback to the captured data.
    date.setCallBack(ondate);
    date.show(getFragmentManager(), "Date Picker");
}

OnDateSetListener ondate = new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        final Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(year, monthOfYear, dayOfMonth);
        millisSinceEpochStart = calendar.getTimeInMillis();
    }
};

private void showDatePickerTimePeriodEnd() {
    final DatePickerFragment date = new DatePickerFragment();
    // Sets up the current date in Dialog.
    final Calendar calender = Calendar.getInstance();
    final Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */
    date.setCallBack(ondateSecond);
    date.show(getFragmentManager(), "Date Picker");
}

OnDateSetListener ondateSecond = new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        final Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(year, monthOfYear, dayOfMonth);
        millisSinceEpochEnd = calendar.getTimeInMillis();
    }
};

The Fragment(s):

public class DatePickerFragment extends DialogFragment {
    OnDateSetListener ondateSet;
    public DatePickerFragment() {}

    public void setCallBack(OnDateSetListener ondate) {
        ondateSet = ondate;
    }
    private int year;
    private int month;
    private int day;

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        year = args.getInt("year");
        month = args.getInt("month");
        day = args.getInt("day");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
    }
}

And there is one more Fragment, which is the same.

So it appears to me, if I set up the first Date, I need to do setCallBack, but I have two separate buttons, so to my understanding, I need another method, which sets another callback. I want to avoid this repetition because it's not very DRY.

How do I fix this?


回答1:


Technically, since it's setting different vars, it is different functionality and is not doing too much repetition. One way to condense the replicated code would be to move it to another method. Try this:

public long getMillis(int year, int monthOfYear, int dayOfMonth){
    final Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(year, monthOfYear, dayOfMonth);
    return calendar.getTimeInMillis();
}

Then you can simply have your callbacks call the above method.

OnDateSetListener ondate= new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        millisSinceEpochStart = getMillis(year, monthOfYear, dayOfMonth);
    }
};

...

OnDateSetListener ondateSecond = new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        millisSinceEpochEnd = getMillis(year, monthOfYear, dayOfMonth);
    }
};



回答2:


If I understand this correctly (if not let me know and I will delete this), you could do something like this

public class DatePickerFragment extends DialogFragment{

    // What you already have
    ...

    // Show the date time picker on click
    // (assuming the click listener is already setup)
    public void showDateTimePicker(){
        final DatePickerFragment date = new DatePickerFragment();
        // Sets up the current date in Dialog.
        final Calendar calender = Calendar.getInstance();
        final Bundle args = new Bundle();
        args.putInt("year", calender.get(Calendar.YEAR));
        args.putInt("month", calender.get(Calendar.MONTH));
        args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
        date.setArguments(args);
        /**
         * Set Call back to capture selected date
         */
        date.setCallBack(getCallback());
        date.show(getFragmentManager(), "Date Picker");
    }

    public OnDateSetListener getCallback(){
        return new OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                final Calendar calendar = Calendar.getInstance();
                calendar.clear();
                calendar.set(year, monthOfYear, dayOfMonth);
                setTime(calendar.getTimeInMillis());
            }
        };
    }

    public void setTime(long timeInMillis){
        // Override this method and do what you want with the millis
    }
}

Then all you would have to do in your DatePickerFragment's is override setTime(long timeInMillis) and do what you want with the value.

For example

public class StartDatePickerFragment extends DatePickerFragment{

    @Override
    public void setTime(long timeInMillis){
        millisSinceEpochStart = timeInMillis;
        // Do whatever else you want in the override call
        ...
    }

    // Do whatever else you want outside of the method
    ...
}

You can also override the getCallback() function itself if you didn't want to utilize the default listener.



来源:https://stackoverflow.com/questions/37124323/i-have-two-methods-which-are-nearly-the-same-how-to-refactor-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!