在我的 Android 应用程序中,我有一些非常相似的类,我们称它们为 FooA
和 FooB
。
对于这些类中的每一个,我都有一个模式类,其中包含表列的常量 - FooASchema
和 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 = ...
}
FooA
和 FooB
都有一个静态工厂方法,使我能够使用 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, ...);
}
最后,我有两个用于从表中检索数据的实用程序类:
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;
}
// ...
}
你可以看到FooA
相关类和FooB
相关类之间的大部分代码非常相似,尤其是在util类中——代码所在几乎相同。
我想尝试减少这种重复,并且我一直在尝试使用泛型来做到这一点(我已经阅读过它们,但我还没有在项目中使用它们)。
例如,我希望能够拥有一个通用的 util 类。以下是我认为我可以实现它的方式:
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();
}
// ...
}
地点:
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 { ... }
但是如你所见,我不能做T.from(cursor)
,我也不能有子类可以实现的抽象常量TABLE_NAME
.
如何以这种方式调用我的静态工厂方法?
有没有更好的方法来解决这个问题并减少代码重复?
最佳答案
在您的实际代码中,您不使用该类的实例来调用 form()
工厂,而是使用该类的静态方法:
fooAs.add(FooA.from(cursor));
对于泛型,您不能像 items.add(T.from(cursor));
那样使用参数化类型调用方法,因为泛型在编译后被删除。
在您的情况下,我看到了两种处理问题的方法:
引入一个带有公共(public)方法的抽象基类和一个子类必须实现的抽象方法来创建一个
Foo
实例(FooA
,FooB
).保持你的做事方式并引入一个接口(interface)来创建一个
Foo
实例。 你会有两个实现。一个用于FooA
,另一个用于FooB
,您可以在FooUtils.get()
方法中提供它的一个实例。
使用第一个选项,您可以执行以下操作。
基类
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);
}
}
使用第二个选项,您可以执行以下操作。
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 已更新,以便 get()
将 FooProcessing
工厂实例作为参数。
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;
}
您现在可以通过这种方式调用 FooUtils.get() 方法:
...
FooProcessing fooAProcessing = new FooAProcessing();
...
ArrayList<FooA> fooAs = FooAUtils.getFoo(context, category, fooAProcessing);
关于java - 在 Java 中使用静态工厂方法和常量进行生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42067114/