策略模式 一、UML類圖 二、實現 三、優點 四、 缺點 五、Android中的源碼分析

Android知識總結

一、UML類圖

策略模式定義了一系列算法,並將每個算法封裝起來,使他們可以相互替換,且算法的變化不會影響到使用算法的客戶。需要設計一個接口,爲一系列實現類提供統一的方法,多個實現類實現該接口,設計一個抽象類(可有可無,屬於輔助類),提供輔助函數。策略模式的決定權在用戶,系統本身提供不同算法的實現,新增或者刪除算法,對各種算法做封裝。因此,策略模式多用在算法決策系統中,外部用戶只需要決定用哪個算法即可。

角色說明:

  • Stragety(抽象策略類):抽象類或接口,提供具體策略類需要實現的接口。
  • ConcreteStragetyA、ConcreteStragetyB(具體策略類):具體的策略實現,封裝了相關的算法實現。
  • Context(環境類):用來操作策略的上下文環境。

二、實現

1. 創建抽象策略類

    public interface ChaseStragety {//追求策略
         void chase();//抽象追求方法
    }

2. 創建具體策略類

    public class ShoppingStrategy implements ChaseStragety {
        @Override
        public void chase() {
            System.out.println("一起逛街咯~");
        }
    }

    public class MoviesStrategy implements ChaseStragety {
        @Override
        public void chase() {
            System.out.println("一起看電影咯~");
        }
    }

    public class EattingStrategy implements ChaseStragety {
        @Override
        public void chase() {
            System.out.println("一起喫飯咯~");
        }
    }

3. 創建環境類

    public class Context { 
        private ChaseStragety chaseStragety;//定義抽象策略類
        public Context(ChaseStragety chaseStragety) {//構造方法傳遞具體策略對象過來
            this.chaseStragety = chaseStragety;
        }
        public void chase(){//執行具體策略對象的策略
            chaseStragety.chase();
        }
    }

4. 測試方法

    public void test(){
        Context context; 
        System.out.println("遇到愛逛街的妹子:");
        context=new Context(new ShoppingStrategy());
        context.chase();

        System.out.println("遇到愛看電影的妹子:");
        context=new Context(new MoviesStrategy());
        context.chase();

        System.out.println("遇到喫貨妹子:");
        context=new Context(new EattingStrategy());
        context.chase();
        
    }

輸出結果:

遇到愛逛街的妹子:
一起逛街咯~
遇到愛看電影的妹子:
一起看電影咯~
遇到喫貨妹子:
一起喫飯咯~

三、優點

  • 策略類可以互相替換
    由於策略類都實現同一個接口,因此他們能夠互相替換。
  • 耦合度低,方便擴展
    增加一個新的策略只需要添加一個具體的策略類即可,基本不需要改變原有的代碼,符合開閉原則。
  • 避免使用多重條件選擇語句(if-else或者switch)。

四、 缺點

  • 策略的增多會導致子類的也會變多
  • 客戶端必須知道所有的策略類,並自行決定使用哪一個策略類。

五、Android中的源碼分析

我們用的ListView時都需要設置一個Adapter,而這個Adapter根據我們實際的需求可以用ArrayAdapter、SimpleAdapter等等,這裏就運用到策略模式。

1. ListView的簡單用法

 listView = (ListView)findViewById(R.id.list_view);
    
    //使用ArrayAdapter
    listView.setAdapter(new ArrayAdapter<String>(this,R.id.item,new String[] {"one","two"}));
    
     //使用BaseAdapter
    listView.setAdapter(new BaseAdapter() {
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    });

2. 相關源碼分析

 public class ListView extends AbsListView {//相當於環境類
        @Override
        public void setAdapter(ListAdapter adapter) {//設置策略,即adapter
            //其他代碼略
        }
    }

    public interface ListAdapter extends Adapter {//抽象策略接口
        
    }
    public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {//具體策略類BaseAdapter,實現ListAdapter接口
        
    }
    public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter {//具體策略類ArrayAdapter,繼承BaseAdapter,即實現ListAdapter接口
        
    }

3. 總結

  • 通過設置不同的Adapter(即不同的策略),我們就可以寫出符合我們需求的ListView佈局。
  • 另外,動畫中的插值器(ValueAnimator 的 setInterpolator 方法)也是有運用到策略模式。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章