I have a list of subclasses which are all instance of Super class. My goal is to create a factory, which will return different result depending on subclass.
public class SuperClass {
...
}
public class SubClass1 extends SuperClass {
...
}
public class SubClass2 extends SuperClass {
...
}
public class Factory {
public static getInstance(SubClass1 item) {
return new EditText();
}
public static getInstance(SubClass2 item) {
return new CheckBox();
}
}
public class Generator {
public Generator() {
List<SuperClass> list = getList();
for (SuperClass item : list) {
Factory.getInstance(item);
}
}
List<SuperClass> getList() {
...
}
}
new Generator();
This code will fail during compilation as it will require getInstance(SuperClass item) overloading, but if I add it then it always will be called.
Is there a way to do this without touching SuperClass, SubClass1, SubClass2?
UPD. In order to clarify what I want to archive here is the original code:
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import com.tom_roush.pdfbox.cos.COSArray;
import com.tom_roush.pdfbox.cos.COSDictionary;
import com.tom_roush.pdfbox.cos.COSName;
import com.tom_roush.pdfbox.pdmodel.PDDocument;
import com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog;
import com.tom_roush.pdfbox.pdmodel.PDPage;
import com.tom_roush.pdfbox.pdmodel.PDPageTree;
import com.tom_roush.pdfbox.pdmodel.common.PDRectangle;
import com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm;
import com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckbox;
import com.tom_roush.pdfbox.pdmodel.interactive.form.PDField;
import com.tom_roush.pdfbox.pdmodel.interactive.form.PDRadioButton;
import com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField;
public class Page {
private View view;
private Context context;
private PDDocument file;
public Page(Context _context, View _view, PDDocument _document) {
view = _view;
context = _context;
document = _document;
renderFields();
}
private void renderFields() {
PDDocumentCatalog docCatalog = document.getDocumentCatalog();
RelativeLayout ll = view.findViewById(R.id.pageFields);
ll.removeAllViews();
PDPageTree pageTree = docCatalog.getPages();
PDPage page = pageTree.get(pageIndex);
PDAcroForm acroForm = docCatalog.getAcroForm();
List<PDField> fields = acroForm.getFields();
for (PDField field : fields) {
String fieldName = field.getFullyQualifiedName();
COSDictionary fieldDict = field.getCOSObject();
COSArray fieldAreaArray = (COSArray) fieldDict.getDictionaryObject(COSName.RECT);
PDRectangle mediaBox = new PDRectangle(fieldAreaArray);
int fieldColor = Color.argb(180, 220, 228, 254);
// Factory
View fieldView = FieldFactory.getViewFromPDField(context, field);
RelativeLayout.LayoutParams fieldLayoutParams = new RelativeLayout.LayoutParams(
(int) (mediaBox.getWidth() * posRatio),
(int) (mediaBox.getHeight() * posRatio)
);
fieldLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
fieldLayoutParams.leftMargin = (int) (left * posRatio);
fieldLayoutParams.topMargin = (int) (top * posRatio);
fieldView.setBackgroundColor(fieldColor);
fieldView.setLayoutParams(fieldLayoutParams);
ll.addView(fieldView, fieldLayoutParams);
}
}
}
...
public class FieldFactory {
public static View getViewFromPDField(Context context, PDTextField field) {
return new EditText(context);
}
public static View getViewFromPDField(Context context, PDCheckbox field) {
return new CheckBox(context);
}
public static View getViewFromPDField(Context context, PDRadioButton field) {
return new RadioButton(context);
}
}