android应用开发设计模式之策略模式
策略模式: 指对象有某个行为,但是在不同的场景中,该行为有不同的实现演算法。
新建一个轮胎接口:
[java]
package com.jindegege.strategy_inte易做图ce;
public inte易做图ce tyre_inte易做图ce {
public String print_tyre_line();// 显示出轮胎的痕迹
}
package com.jindegege.strategy_inte易做图ce;
public inte易做图ce tyre_inte易做图ce {
public String print_tyre_line();// 显示出轮胎的痕迹
}
新建2个轮胎接口的实现类:
[java]
package com.jindegege.tyre_impl;
import com.jindegege.strategy_inte易做图ce.tyre_inte易做图ce;
public class Tyre_long_impl implements tyre_inte易做图ce{
@Override
public String print_tyre_line() {
// TODO Auto-generated method stub
return "在路面上显示一个长轮胎痕迹";
}
}
package com.jindegege.tyre_impl;
import com.jindegege.strategy_inte易做图ce.tyre_inte易做图ce;
public class Tyre_long_impl implements tyre_inte易做图ce{
@Override
public String print_tyre_line() {
// TODO Auto-generated method stub
return "在路面上显示一个长轮胎痕迹";
}
}
[java]
package com.jindegege.tyre_impl;
import com.jindegege.strategy_inte易做图ce.tyre_inte易做图ce;
public class Tyre_short_impl implements tyre_inte易做图ce {
@Override
public String print_tyre_line() {
// TODO Auto-generated method stub
return "在路面上显示一个短轮胎痕迹";
}
}
package com.jindegege.tyre_impl;
import com.jindegege.strategy_inte易做图ce.tyre_inte易做图ce;
public class Tyre_short_impl implements tyre_inte易做图ce {
@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_inte易做图ce.tyre_inte易做图ce;
public class Car {
private String make_address;// 制造地
private int death_year;// 车子使用年限
private int speed;// 速度
private tyre_inte易做图ce tyre_inte易做图ce_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_inte易做图ce getTyre_inte易做图ce_ref() {
return tyre_inte易做图ce_ref;
}
public void setTyre_inte易做图ce_ref(tyre_inte易做图ce tyre_inte易做图ce_ref) {
this.tyre_inte易做图ce_ref = tyre_inte易做图ce_ref;
}
public Map<String,String> start() {
Map<String,String> data=new HashMap<String,String>();
data.put("data1", "Car 起动了!");
data.put("data2", "Car高速行驶,遇到一个大转弯,路面显示:"+this.getTyre_inte易做图ce_ref().print_tyre_line());
return data;
}
}
package com.jindegege.car;
import java.util.HashMap;
import java.util.Map;
import com.jindegege.strategy_inte易做图ce.tyre_inte易做图ce;
public class Car {
private String make_address;// 制造地
private int death_year;// 车子使用年限
private int speed;// 速度
private tyre_inte易做图ce tyre_inte易做图ce_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_inte易做图ce getTyre_inte易做图ce_ref() {
return tyre_inte易做图ce_ref;
}
public void setTyre_inte易做图ce_ref(tyre_inte易做图ce tyre_inte易做图ce_ref) {
this.tyre_inte易做图ce_ref = tyre_inte易做图ce_ref;
}
public Map<String,String> start() {
Map<String,String> data=new HashMap<String,String>();
data.put("data1", "Car 起动了!");
data.put("data2", "Car高速行驶,遇到一个大转弯,路面显示:"+this.getTyre_inte易做图ce_ref().print_tyre_line());
return data;
}
}
新建一个android客户端xml文件以及一个activity类
[html]
<?xml version="1.0"
补充:移动开发 , Android ,