我有两种几乎相同的方法,如何重构它们? [英] I have two methods which are nearly the same, how to refactor them?

查看:91
本文介绍了我有两种几乎相同的方法,如何重构它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 DatePickerFragment s,因为我需要选择开始时间和结束时间。

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

片段:

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.

在我看来,如果我设置了第一个Date,则需要执行 setCallBack ,但是我有两个单独的按钮,所以据我所知,我需要另一个方法,该方法设置另一个回调。我想避免这种重复,因为它不是很干。

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.

我该如何解决?

推荐答案

如果我理解正确(如果不告诉我,我将其删除),则可以执行以下操作

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

然后,在DatePickerFragment中要做的就是覆盖setTime(longtimeInMillis)并执行您想要的操作

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

例如

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()函数本身不想使用默认的监听器。

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

这篇关于我有两种几乎相同的方法,如何重构它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆