小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android 中的工廠模式(減少 if-else 和 switch-case)

 codingSmart 2021-10-22

前言

Android 中的工廠模式,目的就是減少 if-else 和 switch-case 的出現(xiàn),梳理代碼的條理,使邏輯更加清晰,下面直接上代碼:

1.首先新建一個(gè)接口,用來監(jiān)聽我們的操作

public interface StrategyListenter {
   /**
    *
    * @param context 上下文
    * @param result  需要操作的數(shù)據(jù)
    * @param isHandled   判斷是否進(jìn)行下一步操作
    */

   void execute(Context context, int result, boolean[] isHandled);
}

2.重點(diǎn)來了,策略工廠類

public class StrategFactory {
   private List<StrategyListenter> list = new ArrayList<>();
   private static StrategFactory strategFactory = new StrategFactory();

   /**
    * 單列模式
    *
    * @return
    */

   public static StrategFactory init() {
       return strategFactory;
   }

   /**
    * 添加具體操作的類
    *
    * @param listenter
    * @return
    */

   public StrategFactory add(StrategyListenter listenter) {
       if (listenter != null) {
           list.add(listenter);
       }
       return this;
   }

   /**
    * 調(diào)用接口進(jìn)行操作
    *
    * @param context
    * @param result  可根據(jù)實(shí)際情況進(jìn)行修改
    */

   public void Scheduled(Context context, int result) {
       boolean[] isHandled = {false};
       for (StrategyListenter listenter : this.list) {
           if (isHandled[0]) {
               return;
           }
           listenter.execute(context, result, isHandled);

       }

   }

}

3.在Application中聲明

public class MyApplication extends Application {

   @Override
   public void onCreate() {
       super.onCreate();
       StrategFactory strategFactory = StrategFactory.init();
       strategFactory.add(new NumberOne());
       strategFactory.add(new NumberTwo());
       strategFactory.add(new NumberThree());
       strategFactory.add(new NumberFour());
   }
}

4.具體的操作類,在這里面可以執(zhí)行我們想要的操作

public class NumberOne implements StrategyListenter {
   @Override
   public void execute(Context context, int result, boolean[] isHandled) {
       if (result >= 0 && result < 60) {
           DilaogHelper.getDilaog(context, "提示信息", "不及格");
           isHandled[0] = false;
       }
   }
}

5.最后在MianActivity中應(yīng)用

public class MainActivity extends AppCompatActivity {
   private EditText ed_input;
   private Button btn_submit;
   private int strInputNumber;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       ed_input = (EditText) findViewById(R.id.ed_input);
       btn_submit = (Button) findViewById(R.id.btn_submit);
       btn_submit.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               strInputNumber = Integer.valueOf(ed_input.getText().toString());
               StrategFactory.init().Scheduled(MainActivity.this, strInputNumber);
           }
       });
   }
}

6.自己封裝的一個(gè)對話框幫助類

public class DilaogHelper {

   public static void getDilaog(Context context, String title, String message){
       new AlertDialog.Builder(context)
               .setTitle(title)
               .setMessage(message)
               .setPositiveButton("確定", null)
               .show();
   }

}

附:代碼資源下載地址:
http://download.csdn.net/download/qq_32376365/9942966

與之相關(guān)

仿房產(chǎn)銷冠 APP 銷控表界面-多 RecyclerView 同步滾動(dòng)

Android 實(shí)現(xiàn) dialog 的 3D 翻轉(zhuǎn)

微信號(hào):code-xiaosheng

公眾號(hào)

「code小生」

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多