java—我有两个几乎相同的方法,如何重构它们?

lp0sw83n  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(385)

我要吃两个 DatePickerFragment 因为我需要一个开始和结束的时间。

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();
    }
};

碎片:

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);
    }
}

还有一个片段,是一样的。
所以在我看来,如果我安排了第一次约会,我需要 setCallBack ,但是我有两个单独的按钮,所以据我所知,我需要另一个方法,它设置另一个回调。我想避免这种重复,因为它不是很干。
我该怎么解决这个问题?

v09wglhw

v09wglhw1#

从技术上讲,因为它设置了不同的变量,所以它的功能不同,不会重复太多。压缩复制代码的一种方法是将其移动到另一个方法。试试这个:

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

然后您可以简单地让回调调用上述方法。

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);
    }
};
iyzzxitl

iyzzxitl2#

如果我理解正确(如果不让我知道,我会删除这个),你可以这样做

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
    }
}

那么,在datepickerfragment中所要做的就是重写settime(long timeinmillis)并对该值执行所需的操作。
例如

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
    ...
}

如果不想使用默认侦听器,也可以重写getcallback()函数本身。

相关问题