0

メソッドに戻るAsyncTask doInBackground複数のネストされた条件があり、コードが非常に複雑になります。この問題を処理する別の方法はありますかStringonPostExectue

今私のコードは次のようになります

protected String doInBackground(Void... params){
    if (position == 0){ 
         return "Set1" 
    }
    else if(Position >something) { 
         return set 2 
    } .........and so on 
}

@Override
protected void onPostExecute(String result) {
    if (result.equals("set1")){
       // do some taskk
    }
    elseif(result.equal("Set2")){
        // do other task
    }
    else if(){
       // sooo onnnnnn
     }
} 

あなたの助けを前もって感謝します

4

3 に答える 3

1

条件を含むコードを簡素化する OO の優れた方法は、ストラテジー パターン (http://en.wikipedia.org/wiki/Strategy_pattern) を使用してそれらを置き換えることです。この特定のリファクタリングに関する情報は、http: //www.industriallogic.com/xp/refactoring/conditionalWithStrategy.htmlにあります。

基本的な考え方は、各条件付きケースのロジックを戦略でカプセル化し、代わりに戦略インスタンスに委譲することです。これにより、ネストされた if/then/else または switch ステートメントよりもはるかに明確なコードが生成されます。

この点を説明するために、次のような複雑な条件付きロジックがあると仮定しましょう。

Entity e = // some entity
if ("TypeOne".equals(e.getType()) {
  // process entity of type one...
} else if ("TypeTwo".equals(e.getType()) {
  // process entity of type two...
} else if ("TypeThree".equals(e.getType()) {
  // process entity of type three...
} else {
  // default processing logic
}

このロジックを手続き的に記述する代わりに、ストラテジー パターンを使用して、さまざまなエンティティ処理ストラテジーに分解することができます。最初に、すべてのエンティティ処理戦略から共有されるインターフェイスを定義する必要があります。

public interface EntityProcessingStrategy {
  public void process(Entity e);
}

次に、条件付きケースごとに具体的な戦略の実装を作成し、特定の処理ロジックをカプセル化します。

public class TypeOneEntityProcessingStrategy {
  public void process(Entity e) {
    // process entity of type one...
  }
}

public class TypeTwoEntityProcessingStrategy {
  public void process(Entity e) {
    // process entity of type two...
  }
}

public class TypeThreeEntityProcessingStrategy {
  public void process(Entity e) {
    // process entity of type three...
  }
}

public class DefaultEntityProcessingStrategy {
  public void process(Entity e) {
    // default entity processing logic...
  }
}

したがって、前のコードは次のように条件を削除して単純化できます。

Entity e = // our entity that needs to be processed
EntityProcessingStrategy strategy = EntityProcessingStrategies.getStrategyFor(e.getType);
strategy.process(e);

最後の例では、具体的な戦略のファクトリとして機能する EntityProcessingStrategies クラスが含まれていることに注意してください。より具体的には、次のようになります。

public final class EntityProcessingStrategies {

  private EntityProcessingStrategies() { }

  public EntityProcessingStrategy getStrategyFor(String type) {
    if ("TypeOne".equals(type)) return new TypeOneEntityProcessingStrategy();
    if ("TypeTwo".equals(type)) return new TypeTwoEntityProcessingStrategy();
    if ("TypeThree".equals(type)) return new TypeThreeEntityProcessingStrategy();
    return new DefaultEntityProcessingStrategy();
  }
}

これは、具体的な戦略インスタンスを作成する 1 つの方法ですが、決して唯一の方法ではありません。

于 2012-09-26T06:44:28.933 に答える
1

ここで戦略パターンを適用してみることができます。Actionたとえば、抽象関数を使用して抽象クラスを作成できますperform()Action次に、onPostExecute で実行できるすべてのアクションの具体的なサブクラスを作成できます。したがって、クラスにはActionForSet1ActinForSet2 perform() の具体的な実装を含む などがあります。次に、本質的に同じ if/else または break であるが、はるかに保守しやすいAction静的メソッドが必要です。String Action createfromString(String set)すべての文字列に対応する Action サブクラスを返します。そこで if/else の代わりに Map を使用すると、要素の追加/削除が簡単になります。次に、Actionオブジェクトが返された後Action.createFromString、そのオブジェクトを呼び出しますperform()

onPostExecute(String result) {
   Action a = Action.actionForResult(result);
   a.perform();
}

abstract class Action {
  abstract void perform();

  static Action actionForResult(String result) {
      if(result.equals("res1") {
          return new ActionForRes1();
      } else if(result.equals("res2") {
          return new ActionForRes2();
      } else throw new IllegalArgumentException("No action for result " + result);
   }
}

class ActionForResult1 extends Action {
  @Override
  void perform() { Log.i("ACTION", "Here's action for result 1"); }
}

class ActionForResult2 extends Action {
  @Override
  void perform() { Log.i("ACTION", "Here's action for result 2"); }
}
于 2012-09-26T06:09:26.580 に答える
0

このタイプのフローしか使用していない場合

if(){
}
else if(){
}
else if(){
}

次に、代替はフローのタイプのスイッチケースです。doInBackground() から String ではなく整数として返し、スイッチで整数値を使用する必要があります。

switch(result){
case 0:
    break;
case 1:
    break;
....
}

ここで言及したように、ネストされています。実際にはネストされていません。ネストされたブロックの 1 つにも if 条件のようなものがあります

if(){
}
else{
    if(){
    }
    else{
        if(){
        } and so on..
    }
}
于 2012-09-26T05:18:36.557 に答える