0

I have a class which has a static inner class. The OuterClass wants to use the variables from the static inner class. The problem is I need to instantiate the inner class if I'm using instance variables. So, I decided to use a static variables. Does it contrary to OOP concepts? If so, is there any other principle I should follow or any design pattern I should use to do the same thing?

The reason I used static class is I want to create a custom builder for android activity. The problem is I cannot use constructor to initialise OuterClass which extends Activity. So, I need to load those static variables inside the onCreate() method.

This is my sample code

public class DialogFactory extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setBackgroundDrawable(
        new ColorDrawable(android.graphics.Color.TRANSPARENT));

    setContentView(R.layout.activity_custom_dialog);
    this.setDialogTitle(Builder.title);
    this.setDialogMessage(Builder.message);
    this.loadButtons();
}

public void onClick(View view) {
    switch (view.getId()) {
    case R.id.dialog_positive_button:
        Builder.callable.onClickButton(Builder.type, DialogEventListener.ButtonType.POSITIVE_BUTTON);
        this.finish();
        break;
    case R.id.dialog_neutral_button:
        Builder.callable.onClickButton(Builder.type, DialogEventListener.ButtonType.NEUTRAL_BUTTON);
        this.finish();
        break;
    case R.id.dialog_negative_button:
        Builder.callable.onClickButton(Builder.type, DialogEventListener.ButtonType.NEGATIVE_BUTTON);
        this.finish();
        break;
    }
}

private void setDialogTitle(String title) {
    TextView view = (TextView) findViewById(R.id.dialog_title);
    view.setText(title);
}

private void setDialogMessage(String message) {
    TextView view = (TextView) findViewById(R.id.dialog_message);
    view.setText(message);
}

private void loadButtons() {
    Button positiveButton = (Button) findViewById(R.id.dialog_positive_button);
    Button negativeButton = (Button) findViewById(R.id.dialog_negative_button);
    Button neutralButton = (Button) findViewById(R.id.dialog_neutral_button);
    positiveButton.setVisibility(View.GONE);
    negativeButton.setVisibility(View.GONE);
    neutralButton.setVisibility(View.GONE);

    for (Map.Entry<DialogEventListener.ButtonType, String> entry: Builder.buttons.entrySet()) {
        if (entry.getKey() == DialogEventListener.ButtonType.POSITIVE_BUTTON) {
            positiveButton.setVisibility(View.VISIBLE);
            positiveButton.setText(entry.getValue());
        }
        else if (entry.getKey() == DialogEventListener.ButtonType.NEGATIVE_BUTTON) {
            negativeButton.setVisibility(View.VISIBLE);
            negativeButton.setText(entry.getValue());
        }
        else if (entry.getKey() == DialogEventListener.ButtonType.NEUTRAL_BUTTON) {
            neutralButton.setVisibility(View.VISIBLE);
            negativeButton.setText(entry.getValue());
        }
    }
}

@Override
  public void onBackPressed() {
  //
  }

public static final class Builder {
    private static DialogEventListener callable;
    private static DialogEventListener.DialogType type;
    private static String title;
    private static String message;
    private Context context;
    private static Map<DialogEventListener.ButtonType, String> buttons;

    public Builder(Context context, DialogEventListener callable,
        DialogEventListener.DialogType dialogType, String title, String message) {
        Builder.callable = callable;
        Builder.type = dialogType;
        Builder.title = title;
        Builder.message = message;
        this.context = context;
        Builder.buttons = new HashMap<DialogEventListener.ButtonType, String>();

    }

    public Intent build() {
        return new Intent(this.context, DialogFactory.class);
    }

    public void addButton(DialogEventListener.ButtonType buttonType, String label) {
        Builder.buttons.put(buttonType, label);
    }
  }
}
5
  • 1
    it would be easier to understand if you showed real code example instead of this artificial pseudocode. Commented Apr 20, 2014 at 13:15
  • You're almost certainly doing it wrong, but the only way to really learn this stuff is to do it wrong a few (dozen) times. Commented Apr 20, 2014 at 13:17
  • ok @MightyPork I have uploaded my real code. Commented Apr 20, 2014 at 13:28
  • I'm quite lost now tbh. Why are you initializing the static builder fields via constructor? I'm not even sure what you need the Builder class for, actually Commented Apr 20, 2014 at 13:34
  • actually, I'm creating my own custom dialog box. But yes, initialising static fields via constructor is not a good thing. May be I should use a static method to initialise the variables. The code is actually working, the only problem is i'm not sure if it follows the OOP concepts. I don't even think so. That's why I'm asking if there is a better solution to do the same thing. Thanks anyway. Commented Apr 20, 2014 at 13:45

1 Answer 1

2

After looking at your code, I see multiple problems. To start with, the constructor in the Builder class is use less. And accessing all the attributes of the builder class with class name (declaring them as static) will give you uninitialized references and will result into null pointer exception.

I don't completely understand the purpose of your Builder class here, but if possible try to create a separate class that deals with the creation and initialization. After that create an instance of Builder class inside activity class, use constructor to inject dependencies, and try to use functions inside builder class to perform further operations.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your comment, but the problem is I can't use constructor to start a new activity. I need to use Intent intent = new Intent(context, DialogFactory.class); startActivity(intent);
When you call startActivity, the onCreate function of the destination activity will be called. You can instantiate Builder object inside onCreate function.
yes, but I can't pass any variables (for example title, message, DialogEventListener) if I instantiate Builder object inside the onCreate method ,unless I use putextras and getextras

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.