簡體   English   中英

使用 Java 中的靜態工廠方法和常量進行生成

[英]Generifying with static factory methods and constants in Java

在我的 Android 應用程序中,我有一些非常相似的類,我們稱它們為FooAFooB

對於這些類中的每一個,我都有一個架構類,其中包含表列的常量 - FooASchemaFooBSchema

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 = ...

}

FooAFooB都有一個靜態工廠方法,使我能夠使用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, ...);
}

最后,我有兩個 util 類可用於從表中檢索數據:

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)); 因為泛型在編譯后被刪除了。

在您的情況下,我看到兩種處理問題的方法:

  • 引入具有公共方法的抽象基類和子類必須實現以創建Foo實例( FooAFooB )的抽象方法。

  • 保持你的方式並引入一個接口來創建一個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();
    }

    // ...

}

FooA處理

public class FooAProcessing extends AbstractFooProcessing<FooA>{

    @Override
    public FooA createFooInstance(Cursor cursor) {
        return FooA.from(cursor);
    }

}

FooB處理

public class FooBProcessing extends AbstractFooProcessing<FooB>{

    @Override
    public FooB createFooInstance(Cursor cursor) {
        return FooB.from(cursor);
    }

}

使用第二個選項,您可以執行以下操作。

FooProcessing 接口

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM