0

テキスト ファイルをオブジェクトに変換し、それらの値を sqlite データベースに挿入する 3 つの個別のファイル解析関数があります。オブジェクトクラスを除いて、それらはすべて基本的に同じです。

プロセスは次のようになります。

  1. http を使用してファイルをダウンロードする
  2. 進捗計算のためにファイル内の行を数えます
  3. ターゲット テーブルの以前のレコードをすべて削除する
  4. BufferedReader でファイルを開く
  5. 一度に 2000 行を読み取り、オブジェクトに変換する
  6. トランザクションで sqlite に 2000 レコードを挿入する
  7. 完了するまでループ

このコードをジェネリックにして、任意のクラスをオブジェクトの作成に使用できるようにし、データの永続化に使用する DAL 関数を決定する方法がわかりません。Java は私の最初の言語ではないので、どんなガイダンスも素晴らしいものになるでしょう。

私が使用しているコードは次のとおりです。

public void downloadPendingPoleInspections() {

    int count;
    String filePath;
    Inspections inspections = Inspections.getInstance();

    filePath = Environment.getExternalStorageDirectory() + File.separator + "inspections.txt";

    try {

        downloadFile("http://localhost/api/inspectionservices.aspx?o=retrieve", "pendinginspections.txt", POST_PENDING_INSPECTIONS_PROGRESS_UPDATE);

        int totalInspections = getLineCount(filePath);

        inspections.deleteAllPendingInspections();          

        File file = new File(filePath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        int i = 0;
        int j = 0;

        List<PendingInspection> batch = new ArrayList<PendingInspection>();

        while ((line = br.readLine()) != null) {
            String[] values = line.split(" ");

            PendingInspection pending = new PendingInspection(
                    Integer.parseInt(values[0]), values[1],
                    Double.parseDouble(values[2]),
                    Double.parseDouble(values[3]));

            batch.add(pending);
            i++;
            j++;

            if (i >= 2000) {

                inspections.pendingInspectionsBatchInsert(batch);                   
                batch.clear();
                i = 0;                  
            }
        }

        if (i > 0) {
            inspections.pendingInspectionsBatchInsert(batch);
            batch.clear();
        }

        br.close();
        file.delete();

    } catch (Exception e) {
        Log.e("SyncActivity", e.toString());            
    }       
}

編集:これはインターフェースとクラスの宣言です

public interface Inspectable {
    public int getId();
    public void setId(int id);

    public String getLabel();
    public void setLabel(String label);

    public double getX();
    public void setX(double x);

    public double getY();
    public void setY(double y);
}

public class RWInspection {
private String id;
private double x;
private double y;
private String inspector;
private String comments;
private String timestamp;


public RWInspection(String id, double x, double y, String inspector, String comments, String timestamp) {
        this.id = id;       
        this.x = x;
        this.y = y;
        this.inspector = inspector;
        this.comments = comments;
        this.timestamp = timestamp;
}

中略....ゲッターとセッターの実装

public class PInspection implements Inspectable{
    private int id;
    private String number;
    private double x;
    private double y;

public PInspection(int id, String poleNumber, double x, double y) {
    this.id = id;
    this.number = number ;
    this.x = x;
    this.y = y;
}
4

2 に答える 2

1

オブジェクトクラスを除いて、それらはすべて基本的に同じです。

バッチ プロセスの一部として作成されるこれらすべてのオブジェクトに共通のインターフェイスが必要なようです。次のようなものをお勧めします。

public interface Batchable  {  void doBatch();  }

次に、次の操作を行います。

public class Foo implements Batchable  {}  
public class Bar implements Batchable {}

これで、各クラスに独自の関数本体を実装させることができdoBatch、クラスを知る必要性を少なくとも部分的に抽象化できました。一度に 2000 件のレコードを永続化するという点では、大規模なトランザクションで一度にすべてをプッシュしてみませんか。これを行わないと、データの整合性が失われる危険があります。

于 2012-10-31T16:12:17.080 に答える
1

これを抽象基本クラスといくつかの実装に分割します。基本クラスは次のようになります。

public abstract class Downloader {
    protected abstract void processLine(String[] line);
    protected abstract void save();
    protected abstract String file();
public void downloadPendingPoleInspections() {

    int count;
    String filePath;

    filePath = Environment.getExternalStorageDirectory() + File.separator + file();

    try {

        downloadFile("http://localhost/api/inspectionservices.aspx?o=retrieve", "pending" + file(), POST_PENDING_INSPECTIONS_PROGRESS_UPDATE);

        int totalInspections = getLineCount(filePath);

        File file = new File(filePath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        int i = 0;
        int j = 0;


        while ((line = br.readLine()) != null) {
            processLine(line.split(" "));
            i++;
            j++;

            if (i >= 2000) {
                save()
                i = 0;                  
            }
        }

        if (i > 0) {
            save()
        }

        br.close();
        file.delete();

    } catch (Exception e) {
        Log.e("SyncActivity", e.toString());            
    }       
}

処理したいタイプごとに、次のような小さな実装を作成します。

public class InspectionDownloader extends DownLoader {
    Inspections inspections = Inspections.getInstance();
    List<PendingInspection> batch = new ArrayList<PendingInspection>();

    public InspectionDownloader() {
        inspections.deleteAllPendingInspections();
    }

    protected void processLine(String[] values) {
        PendingInspection pending = new PendingInspection(
            Integer.parseInt(values[0]), values[1],
            Double.parseDouble(values[2]),
            Double.parseDouble(values[3]));
        batch.add(pending);
    }

    protected void save() {
        inspections.pendingInspectionsBatchInsert(batch);
        batch.clear();
    }
    protected String file() { 
        return "inspections.txt";
    }
}

このようにして、再利用可能なロジックを基本クラスに集中させ、特別なロジックを小規模で焦点を絞った特殊なクラスに移動できます。このパターンはテンプレート方式と呼ばれます。派生クラスが、担当する型に特有の操作に非常に重点を置いていることがわかります。

于 2012-10-31T17:02:40.983 に答える