In my Android app, I have some very similar classes, let's call them FooA
and FooB
.
For each of these classes, I have a schema class that contains constants for the columns of the table - FooASchema
and FooBSchema
:
public final class FooASchema {
public static final String TABLE_NAME = "foo_a_table";
public static final String COL_CATEGORY_ID = "category_id";
public static final String COL_PROPERTY_A = "property_a";
public static final String COL_PROPERTY_B = "property_b";
// COL_PROPERTY_C = ...
}
public final class FooBSchema {
public static final String TABLE_NAME = "foo_b_table";
public static final String COL_CATEGORY_ID = "category_id";
public static final String COL_OTHER_PROPERTY_A = "other_property_a";
// COL_OTHER_PROPERTY_B = ...
}
Both FooA
and FooB
have a static factory method that enables me to create them using a Cursor
:
public static FooA from(Cursor cursor) {
int categoryId = cursor.getInt(cursor.getColumnIndex(FooASchema.COL_CATEGORY_ID));
String propertyA = cursor.getString(cursor.getColumnIndex(FooASchema.COL_PROPERTY_A));
String propertyB = cursor.getString(cursor.getColumnIndex(FooASchema.COL_PROPERTY_B));
// int propertyC = ...
return FooA(id, propertyA, propertyB, ...);
}
public static FooB from(Cursor cursor) {
int categoryId = cursor.getInt(cursor.getColumnIndex(FooBSchema.COL_CATEGORY_ID));
int otherA = cursor.getInt(cursor.getColumnIndex(FooASchema.COL_OTHER_PROPERTY_A));
// String otherB = ...
return FooB(id, otherA, otherB, ...);
}
Finally, I have two util classes that I use to retrieve data from the tables:
public final class FooAUtils {
public static ArrayList<FooA> getFooAs(Context context, int categoryId) {
ArrayList<FooA> fooAs = new ArrayList<>();
Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
FooASchema.TABLE_NAME,
null,
FooASchema.COL_CATEGORY_ID + "=?",
new String[] {String.valueOf(categoryId)},
null,
null,
null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
fooAs.add(FooA.from(cursor));
cursor.moveToNext();
}
cursor.close();
return fooAs;
}
// ...
}
public final class FooBUtils {
public static ArrayList<FooA> getFooBs(Context context, int categoryId) {
ArrayList<FooB> fooBs = new ArrayList<>();
Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
FooBSchema.TABLE_NAME,
null,
FooBSchema.COL_CATEGORY_ID + "=?",
new String[] {String.valueOf(categoryId)},
null,
null,
null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
fooBs.add(FooB.from(cursor));
cursor.moveToNext();
}
cursor.close();
return fooBs;
}
// ...
}
You can see that most of the code between FooA
-related classes and FooB
-related classes are very similar, and especially in the util classes - where the code is almost identical.
I want to try to reduce this duplication, and I have been trying to do so using generics (I've read about them, but I haven't yet used them in a project).
For example, I want to be able to have a generic util class. Here's how I thought I could implement it:
public final class FooUtils {
public static <T> get(Context context, int categoryId) {
ArrayList<T> items = new ArrayList<>();
Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
BaseSchema.TABLE_NAME,
null,
BaseSchema.COL_CATEGORY_ID + "=?",
new String[] {String.valueOf(categoryId)},
null,
null,
null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
items.add(T.from(cursor)); // ??
cursor.moveToNext();
}
cursor.close();
}
// ...
}
Where:
public interface BaseSchema {
public static final String TABLE_NAME; // can't make this abstract?
public static final String COL_CATEGORY_ID = "category_id";
}
public final class FooASchema implements BaseSchema { ... }
public final class FooBSchema implements BaseSchema { ... }
But as you can see, I can't do T.from(cursor)
, and I can't have an abstract constant TABLE_NAME
that the subclasses can implement.
How can I call my static factory method in this way?
Is there a better way of approaching this and reducing code duplication?
In your actual code you don't use an instance of the class to invoke the form()
factory, you use a static method of the class :
fooAs.add(FooA.from(cursor));
With generics, you cannot use the parameterized type to invoke a method on it like that items.add(T.from(cursor));
since the generic was erased after the compilation.
In your case, I see two ways of handling the problem :
introducing a abstract base class with the common method and an abstract method that subclasses have to implement to create a Foo
instance ( FooA
, FooB
).
keeping your way of doing and introducing an interface to create a Foo
instance. You would have two implementation of it. One for FooA
and another one for FooB
and you could provide a instance of it in the FooUtils.get()
method.
With the first option you could do the following.
Base class
public abstract class AbstractFooProcessing<T extends Foo> {
public abstract T createFooInstance(Cursor cursor);
public ArrayList<T> get(Context context, int categoryId) {
ArrayList<T> items = new ArrayList<>();
Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
BaseSchema.TABLE_NAME,
null,
BaseSchema.COL_CATEGORY_ID + "=?",
new String[] {String.valueOf(categoryId)},
null,
null,
null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
items.add(createFooInstance(cursor));
cursor.moveToNext();
}
cursor.close();
}
// ...
}
FooAProcessing
public class FooAProcessing extends AbstractFooProcessing<FooA>{
@Override
public FooA createFooInstance(Cursor cursor) {
return FooA.from(cursor);
}
}
FooBProcessing
public class FooBProcessing extends AbstractFooProcessing<FooB>{
@Override
public FooB createFooInstance(Cursor cursor) {
return FooB.from(cursor);
}
}
With the second option you could do the following.
FooProcessing interface
public interface FooProcessing<T extends Foo> {
T createFooInstance(Cursor cursor);
}
FooProcessingA
public class FooAProcessing implements FooProcessing<FooA>{
@Override
public FooA createFooInstance(Cursor cursor) {
return FooA.from(cursor);
}
}
FooProcessingB
public class FooBProcessing implements FooProcessing<FooB>{
@Override
public FooB createFooInstance(Cursor cursor) {
return FooB.from(cursor);
}
}
FooUtils updated so that get()
takes as argument a FooProcessing
factory instance.
public final class FooUtils {
public static <T extends Foo> ArrayList<T> get(Context context, int categoryId, FooProcessing<T> fooProcessing) {
ArrayList<T> items = new ArrayList<>();
Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
BaseSchema.TABLE_NAME,
null,
BaseSchema.COL_CATEGORY_ID + "=?",
new String[] {String.valueOf(categoryId)},
null,
null,
null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
items.add(fooProcessing.createFooInstance(cursor)); // ??
cursor.moveToNext();
}
cursor.close();
}
// ...
return items;
}
You can now call the FooUtils.get() method in this way :
...
FooProcessing fooAProcessing = new FooAProcessing();
...
ArrayList<FooA> fooAs = FooAUtils.getFoo(context, category, fooAProcessing);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.