Reputation: 119
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);
}
}
}
Upvotes: 0
Views: 276
Reputation: 17401
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.
Upvotes: 2