android應用開發設計模式之策略模式

策略模式: 指對象有某個行為,但是在不同的場景中,該行為有不同的實現演算法。

 新建一個輪胎接口:

[java]
package com.jindegege.strategy_interface; 
 
public interface tyre_interface  { 
    
    public String print_tyre_line();// 顯示出輪胎的痕跡  

package com.jindegege.strategy_interface;

public interface tyre_interface  {
  
    public String print_tyre_line();// 顯示出輪胎的痕跡
}

 

 

新建2個輪胎接口的實現類:

[java]
package com.jindegege.tyre_impl; 
 
import com.jindegege.strategy_interface.tyre_interface; 
 
public class Tyre_long_impl implements tyre_interface{ 
 
    @Override 
    public String print_tyre_line() { 
        // TODO Auto-generated method stub  
        return "在路面上顯示一個長輪胎痕跡"; 
 
    } 
 

package com.jindegege.tyre_impl;

import com.jindegege.strategy_interface.tyre_interface;

public class Tyre_long_impl implements tyre_interface{

 @Override
 public String print_tyre_line() {
  // TODO Auto-generated method stub
  return "在路面上顯示一個長輪胎痕跡";

 }

}
 

 

[java]
package com.jindegege.tyre_impl; 
 
import com.jindegege.strategy_interface.tyre_interface; 
 
public class Tyre_short_impl implements tyre_interface { 
 
    @Override 
    public String print_tyre_line() { 
        // TODO Auto-generated method stub  
        return "在路面上顯示一個短輪胎痕跡"; 
    } 
 

package com.jindegege.tyre_impl;

import com.jindegege.strategy_interface.tyre_interface;

public class Tyre_short_impl implements tyre_interface {

 @Override
 public String print_tyre_line() {
  // TODO Auto-generated method stub
  return "在路面上顯示一個短輪胎痕跡";
 }

}

 

 

基於一個輪胎接口來實現不同樣式的輪胎樣式。

    組裝一個Car車類:

[java]
package com.jindegege.car; 
 
import java.util.HashMap; 
import java.util.Map; 
 
import com.jindegege.strategy_interface.tyre_interface; 
 
public class Car { 
    private String make_address;// 制造地  
    private int death_year;// 車子使用年限  
    private int speed;// 速度  
    private tyre_interface tyre_interface_ref;// 輪胎的樣式  
    public String getMake_address() { 
        return make_address; 
    } 
    public void setMake_address(String make_address) { 
        this.make_address = make_address; 
    } 
    public int getDeath_year() { 
        return death_year; 
    } 
    public void setDeath_year(int death_year) { 
        this.death_year = death_year; 
    } 
    public int getSpeed() { 
        return speed; 
    } 
    public void setSpeed(int speed) { 
        this.speed = speed; 
    } 
    public tyre_interface getTyre_interface_ref() { 
        return tyre_interface_ref; 
    } 
    public void setTyre_interface_ref(tyre_interface tyre_interface_ref) { 
        this.tyre_interface_ref = tyre_interface_ref; 
    } 
    public Map<String,String> start() { 
        Map<String,String> data=new HashMap<String,String>(); 
         
        data.put("data1", "Car 起動瞭!"); 
 
        data.put("data2", "Car高速行駛,遇到一個大轉彎,路面顯示:"+this.getTyre_interface_ref().print_tyre_line()); 
        return data; 
    } 

package com.jindegege.car;

import java.util.HashMap;
import java.util.Map;

import com.jindegege.strategy_interface.tyre_interface;

public class Car {
    private String make_address;// 制造地
    private int death_year;// 車子使用年限
    private int speed;// 速度
    private tyre_interface tyre_interface_ref;// 輪胎的樣式
    public String getMake_address() {
        return make_address;
    }
    public void setMake_address(String make_address) {
        this.make_address = make_address;
    }
    public int getDeath_year() {
        return death_year;
    }
    public void setDeath_year(int death_year) {
        this.death_year = death_year;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public tyre_interface getTyre_interface_ref() {
        return tyre_interface_ref;
    }
    public void setTyre_interface_ref(tyre_interface tyre_interface_ref) {
        this.tyre_interface_ref = tyre_interface_ref;
    }
    public Map<String,String> start() {
     Map<String,String> data=new HashMap<String,String>();
     
        data.put("data1", "Car 起動瞭!");

        data.put("data2", "Car高速行駛,遇到一個大轉彎,路面顯示:"+this.getTyre_interface_ref().print_tyre_line());
        return data;
    }
}

 

新建一個android客戶端xml文件以及一個activity類

[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:id="@+id/textview01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
    <TextView 
        android:id="@+id/textview02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
    <TextView 
        android:id="@+id/textview03" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
      /> 
    <TextView 
        android:id="@+id/textview04" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
    <TextView 
        android:id="@+id/textview05" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
      /> 
    <TextView 
        android:id="@+id/textview06" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView
        android:id="@+id/textview02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/textview03"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      />
    <TextView
        android:id="@+id/textview04"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/textview05"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      />
    <TextView
        android:id="@+id/textview06"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

主activity類:

[java]
import android.widget.TextView; 
 
public class StrategyActivity extends Activity { 
    private TextView textview01; 
    private TextView textview02; 
    private TextView textview03; 
    private TextView textview04; 
    private TextView textview05; 
    private TextView textview06; 
    private Tyre_long_impl tyre_long_impl; 
    private Tyre_short_impl tyre_short_impl; 
    private Map<String,String> data; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        textview01=(TextView)findViewById(R.id.textview01); 
        textview02=(TextView)findViewById(R.id.textview02); 
        textview03=(TextView)findViewById(R.id.textview03); 
        textview04=(TextView)findViewById(R.id.textview04); 
        textview05=(TextView)findViewById(R.id.textview05); 
        textview06=(TextView)findViewById(R.id.textview06); 
        tyre_long_impl = new Tyre_long_impl(); 
        tyre_short_impl = new Tyre_short_impl(); 
        Car car = new Car(); 
         
        car.setDeath_year(10); 
        car.setMake_address("廣東深圳") ; 
        car.setSpeed(250); 
        car.setTyre_interface_ref(tyre_long_impl); 
        if(textview01!=null&&car.getMake_address()!=null){ 
            textview01.setText(car.getMake_address()); 
        } 
        if(textview02!=null){ 
            textview02.setText(new Integer(car.getSpeed()).toString()); 
        } 
        if(textview03!=null){ 
            textview03.setText(new Integer(car.getDeath_year()).toString()); 
        } 
         data= car.start(); 
          
         if(textview04!=null&&data.get("data1")!=null){ 
            textview04.setText(data.get("data1").toString()); 
         } 
         if(textview05!=null&&data.get("data2")!=null){ 
            textview05.setText(data.get("data2").toString()); 
          } 
         
 
    } 

import android.widget.TextView;

public class StrategyActivity extends Activity {
    private TextView textview01;
    private TextView textview02;
    private TextView textview03;
    private TextView textview04;
    private TextView textview05;
    private TextView textview06;
    private Tyre_long_impl tyre_long_impl;
    private Tyre_short_impl tyre_short_impl;
    private Map<String,String> data;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview01=(TextView)findViewById(R.id.textview01);
        textview02=(TextView)findViewById(R.id.textview02);
        textview03=(TextView)findViewById(R.id.textview03);
        textview04=(TextView)findViewById(R.id.textview04);
        textview05=(TextView)findViewById(R.id.textview05);
        textview06=(TextView)findViewById(R.id.textview06);
        tyre_long_impl = new Tyre_long_impl();
        tyre_short_impl = new Tyre_short_impl();
        Car car = new Car();
       
        car.setDeath_year(10);
        car.setMake_address("廣東深圳") ;
        car.setSpeed(250);
        car.setTyre_interface_ref(tyre_long_impl);
        if(textview01!=null&&car.getMake_address()!=null){
         textview01.setText(car.getMake_address());
        }
        if(textview02!=null){
         textview02.setText(new Integer(car.getSpeed()).toString());
        }
        if(textview03!=null){
         textview03.setText(new Integer(car.getDeath_year()).toString());
        }
         data= car.start();
        
         if(textview04!=null&&data.get("data1")!=null){
          textview04.setText(data.get("data1").toString());
         }
         if(textview05!=null&&data.get("data2")!=null){
           textview05.setText(data.get("data2").toString());
          }
       

    }
}[java]
實現的簡單效果: 
實現的簡單效果:[java]
<img alt="" src="/wp-content/images1/20181012/20120530022807297332.gif" /> 
<img alt="" src="/wp-content/images1/20181012/20120530022807297332.gif" /> [java]
 

 讓車跑起來,並且具有更換輪胎樣式的功能:是一個長輪胎痕跡,但在程序中可以使用代碼:car.setTyre_interface_ref(tyre_long_impl);來對輪胎的樣式進行不同的替換,可以替換成短輪胎痕跡的汽車輪胎,這樣在不更改Car類的前題下進行瞭不同輪胎樣式的改變,輪胎和輪胎之間可以互相替換,這就是策略模式。

源代碼下載地址:http://up.aiwalls.com/2012/0530/20120530022756900.rar
 

 

摘自 jindegegesun的專欄

發佈留言