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