2

フォルダ内に3種類のファイルがあります。すべてのファイルには、従業員テーブルに保存される同じデータがあります。

ファイルタイプ:

  1. XML
  2. Excel
  3. 。TXT

2つのメソッドを持つ3つの別々のクラスを作成しました。これは、filePathをStringパラメーターとして受け取り、EmployeeVOObjectまたはを返しますList<EmployeeVO>

そうでなければ、これらを削除したいと思います。

List<EmployeeVO>  list ;
 if(fileName.endsWith(".xml")){
    list =   XmlReader();
 }else if(fileName.endsWith(".EXCEL")){
    list =     ExcelReader();
 }else if(fileName.endsWith(".TXT")){
    list =     TxtReader();
 }
4

7 に答える 7

3

ここでコマンドパターンが使えると思います。これは、新しいオプションを追加するときに無期限に大きくなる傾向がある面倒なスイッチ/ifブロックを置き換えるために使用できます。

別のオプションはファクトリパターンかもしれません。あなたは醜さを処理し、ifの豊富さを隠すファクトリーにあなたのif/スイッチを含めます。

もちろん、両方を組み合わせることができます。それはあなたの好みとあなたがそれを使用している文脈にのみ依存します。

ちなみに、パターンは派手だからといって、必要な場所で使用しないでください。

于 2012-11-15T15:24:20.630 に答える
2

最も単純なソリューションが最良で最も読みやすい場合があります。

switch(FilenameUtils.getExtension(fileName).toUpperCase()) {
    case "XML":
        return XmlReader();
     case "EXCEL":
        return next ExcelReader();
    case "TXT":
        return new TxtReader();
    default:
        //...
}

私はFilenameUtils.getExtension()ソース)を使用しており、Java7から文字列をオンにしています。

于 2012-11-15T15:28:48.997 に答える
2

ある種の戦略を使用できます。

// class field holding association from readers to extension
Map<String, Reader> readers = new HashMap<String, Reader>();

// configuration (bean initialization for example)
readers.put("xml", xmlReader);
readers.put("xls", excelReader);
readers.put("txt", txtReader);

// execution would be something like:
List<EmployeeVO> list = readers.get(fileExtension).readList();
于 2012-11-15T15:28:58.560 に答える
1

ある時点で、if / elseロジックを実行する必要がありますが、これは避けられません。EmployeeFileParser派生クラスを持つクラスと、ファイルのサフィックスに基づいてインスタンス化するものを決定する静的メソッドに移動できます。

于 2012-11-15T15:25:09.457 に答える
1

ファクトリパターンを使用する必要があります。XML、EXCEL、TEXTリーダーは、基本の抽象クラス/インターフェイスリーダーを拡張する必要があります。

Based on the input passed, you should return the corresponding Reader.

public interface Reader {

public void save(File file) throws Exception;

}

public class ReaderFactory {

public Reader getReader(File file) {
    Reader reader = null;
    if xml then reader = new XMLReader();
    else ....
}

}

于 2012-11-15T15:30:09.540 に答える
1

I would let each reader decide what formats it supports. In short:

  • Create a super reader class and, if needed, wrap the specialed classes (ExcelReader, etc) in your own classes
  • Add an abstract canProcess(String filename) in the superclass, and implement it in your specialized classes (it should determine if the specialized readerclass can handle the file)
  • Create a static method in the superclass which loops the reader classes and determines the one to use, and uses it

This way, you can create new readers for any filetype dynamically.

于 2012-11-15T15:33:26.920 に答える
1

It seems to me that you can use Chain of Responsibility Design Pattern. A very nice example, similar to your case, is the following.

http://www.javacodegeeks.com/2012/09/chain-of-responsibility-design-pattern.html

それが役に立てば幸い。

更新:私は答えを見ていましたが、状況によっては単純な解決策が最も好ましく、最も読みやすいものであることに同意します。ただし、デザインパターンを使用してコードを設計する場合は、Chain of Responsibilityには、コード内のすべてのif {}else{}ステートメントが削除されるという利点があります。必要なのは、パーサーと各パーサーの後継のみです。このソリューションにはパフォーマンス上の欠点があるかもしれませんが、コードをより拡張可能にします。場合によっては、行う必要のある取引があるため、プロジェクトの要件を考慮する必要があります。

于 2012-11-15T15:59:39.383 に答える