1

Currently I'm working on an Android library that creates a dialog which allows to select colors from a palette (examples at the end) and I've stumbled upon an interesting design problem.

To create the dialog object, I decided to use the Builder Pattern. One of the methods on the Builderobject is .useMaterial(value), where value is a boolean.

What useMaterial(true) does is sets the code to use the builder from android.support.v7.app.AlertDialog.Builder, otherwise the builder from android.app.AlertDialog.Builder is used.

The method calls on both builders are exactly the same. Here is the code that takes care of the problem (useMaterial is a boolean which specifies which builder to use):

if (useMaterialDialog) {
    return createMaterialDialog(activity, view);
}
else {
    return createDefaultDialog(activity, view);
}

And the two methods:

private android.support.v7.app.AlertDialog createMaterialDialog(Context context, View view) {
    android.support.v7.app.AlertDialog.Builder alertDialogBuilder =
            new android.support.v7.app.AlertDialog.Builder(context);
    alertDialogBuilder
            .setTitle(titleResId)
            .setView(view);

    // Add positve and negative buttons to dialog, if needed
    if (showOkCancelButtons) {
        alertDialogBuilder.setPositiveButton(R.string.dialog_positive_button_text, onOkPressedListener)
                .setNegativeButton(R.string.dialog_negative_button_text, onCancelPressedListener);
    }

    return alertDialogBuilder.create();
}
private android.app.AlertDialog createDefaultDialog(Context context, View view) {
    android.app.AlertDialog.Builder alertDialogBuilder =
            new android.app.AlertDialog.Builder(context);
    alertDialogBuilder
            .setTitle(titleResId)
            .setView(view);

    // Add positve and negative buttons to dialog, if needed
    if (showOkCancelButtons) {
        alertDialogBuilder.setPositiveButton(R.string.dialog_positive_button_text, onOkPressedListener)
                .setNegativeButton(R.string.dialog_negative_button_text, onCancelPressedListener);
    }

    return alertDialogBuilder.create();
}

Now note, both of the methods have duplicate code the only place where they differ is the first line, i.e. where the type of the builder object is decided.

This approach forced me to define onOkPressedListener and onCancelPressedListener as member variables, to avoid further code duplication.

I thought about using the State Pattern, but that seems to be an overkill, since it would add too much code, since the task is pretty simple.

What would be the correct approach here?

P.S. Below are the examples of the "dialogs" mentioned in the question:

Non-Material (when useMaterial is false) non material android color picker dialog

Material (when useMaterial is true) material android color picker dialog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.