策略模式: 指对象有某个行为,但是在不同的场景中,该行为有不同的实现演算法。
新建一个轮胎接口:
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{ @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车类:
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