android应用开发设计模式之策略模式

策略模式: 指对象有某个行为,但是在不同的场景中,该行为有不同的实现演算法。

新建一个轮胎接口:

public interface tyre_interface {
    public String print_tyre_line();// 显示出轮胎的痕迹 
}


 

新建2个轮胎接口的实现类:

public class Tyre_long_impl implements tyre_interface{
    @Override 
    public String print_tyre_line() {
        return "在路面上显示一个长轮胎痕迹";

    }
}

public class Tyre_short_impl implements tyre_interface {
    @Override 
    public String print_tyre_line() {
	return "在路面上显示一个短轮胎痕迹";

    }
}

 

基于一个轮胎接口来实现不同样式的轮胎样式。

组装一个Car车类:

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;
    }
}


新建一个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类:

public class StrategyActivityextends 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());
	}
    }
}


实现的简单效果:

http://hi.csdn.net/attachment/201202/27/0_13303134343MLG.gif

让车跑起来,并且具有更换轮胎样式的功能:是一个长轮胎痕迹,但在程序中可以使用代码:car.setTyre_interface_ref(tyre_long_impl);来对轮胎的样式进行不同的替换,可以替换成短轮胎痕迹的汽车轮胎,这样在不更改Car类的前题下进行了不同轮胎样式的改变,轮胎和轮胎之间可以互相替换,这就是策略模式。

源代码下载地址:http://download.csdn.net/detail/jindegegesun/4093606

source link:http://blog.csdn.net/jindegegesun

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值