策略模式: 指對象有某個行為,但是在不同的情境中,該行為有不同的實現演算法。
建立一個輪胎介面:
package com.jindegege.strategy_interface;public interface tyre_interface { public String print_tyre_line();// 顯示出輪胎的痕迹}
建立2個輪胎介面的實作類別:
package com.jindegege.tyre_impl;import com.jindegege.strategy_interface.tyre_interface;public class Tyre_long_impl implements tyre_interface{@Overridepublic String print_tyre_line() {// TODO Auto-generated method stubreturn "在路面上顯示一個長輪胎痕迹";}}
package com.jindegege.tyre_impl;import com.jindegege.strategy_interface.tyre_interface;public class Tyre_short_impl implements tyre_interface {@Overridepublic String print_tyre_line() {// TODO Auto-generated method stubreturn "在路面上顯示一個短輪胎痕迹";}}
基於一個輪胎介面來實現不同樣式的輪胎樣式。
組裝一個Car車類:
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類
<?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類:
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()); } }}
實現的簡單效果:
讓車跑起來,並且具有更換輪胎樣式的功能:是一個長輪胎痕迹,但在程式中可以使用代碼:car.setTyre_interface_ref(tyre_long_impl);來對輪胎的樣式進行不同的替換,可以替換成短輪胎痕迹的汽車輪胎,這樣在不更改Car類的前題下進行了不同輪胎樣式的改變,輪胎和輪胎之間可以互相替換,這就是策略模式。
原始碼:http://download.csdn.net/detail/jindegegesun/4093606