Android[高级教程] 设计模式之八 桥接模式
现在我们说一下桥接模式,打个比方,桥接模式就好比是小时候玩的搭积木一样,每一块积木都有自己的造型,但将它们组合起来可以实现不同的样式。我们还是拿《西游记》中的主角来解释这个模式,悟空是用金箍棒,八戒是用九齿钉耙,沙僧是用降妖宝杖的,每个使的兵器都是不同的,我们先来看一下例图吧:
好了,我们首先定义一个武器接口:
[java] public inte易做图ce Weapon{
public String getWeapon();
}
public inte易做图ce Weapon{
public String getWeapon();
}接着再定义了一个人的接口,这里面就有使用武器的方法,这里需要说清楚的是:人使用武器,而不是武器使用人,所以我们把接口类放在Person里面:
[java] public inte易做图ce Person {
public void setWeapon(Weapon weapon);
}
public inte易做图ce Person {
public void setWeapon(Weapon weapon);
}接口定义好以后就是每个接口的具体实现方法了:
我们先来定义三种武器:
金箍棒
[java] public class JinGuBang implements Weapon {
private String weapon;
public JinGuBang() {
this.weapon = "金箍棒";
}
@Override
public String getWeapon() {
return weapon;
}
}
public class JinGuBang implements Weapon {
private String weapon;
public JinGuBang() {
this.weapon = "金箍棒";
}
@Override
public String getWeapon() {
return weapon;
}
}九齿钉耙
[java] public class JiuChiDingPa implements Weapon {
private String weapon;
public JiuChiDingPa() {
this.weapon = "九齿钉耙";
}
@Override
public String getWeapon() {
return weapon;
}
}
public class JiuChiDingPa implements Weapon {
private String weapon;
public JiuChiDingPa() {
this.weapon = "九齿钉耙";
}
@Override
public String getWeapon() {
return weapon;
}
}最后就是降妖宝仗
[java] public class XiangYaoBaoZhang implements Weapon {
private String weapon;
public XiangYaoBaoZhang() {
this.weapon = "降妖宝仗";
}
@Override
public String getWeapon() {
return weapon;
}
}
public class XiangYaoBaoZhang implements Weapon {
private String weapon;
public XiangYaoBaoZhang() {
this.weapon = "降妖宝仗";
}
@Override
public String getWeapon() {
return weapon;
}
}三种武器定义好以后就是三个徒弟类的实现了:
[java] public class Wukong implements Person {
private String name;
private Weapon weapon;
public Wukong() {
this.name = "悟空";
}
@Override
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
@Override
public String toString() {
return name + "使用" + weapon.getWeapon();
}
}
public class Wukong implements Person {
private String name;
private Weapon weapon;
public Wukong() {
this.name = "悟空";
}
@Override
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
@Override
public String toString() {
return name + "使用" + weapon.getWeapon();
}
}这里定义了悟空需要武器来打妖怪,但是哪种武器没有具体说明,八戒和沙僧也是一样的,这里就不一一列出了,最后我们看一下Android中的实现:
[java] public class XiyoujiActivity extends Activity {
private ListView listView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
//三个徒弟类www.zzzyk.com
Person wukong = new Wukong();
Person bajie = new Bajie();
Person shasheng = new Shaseng();
//三种武器类
Weapon jinGuBang = new JinGuBang();
Weapon jiuChiDingPa = new JiuChiDingPa();
Weapon XiangYaoBaoZhang = new XiangYaoBaoZhang();
//设置孙悟空的武器为"金箍棒"
wukong.setWeapon(jinGuBang);
//设置八戒的武器为"九齿钉耙"
bajie.setWeapon(jiuChiDingPa);
//设置沙僧的武器为"降妖宝杖"
shasheng.setWeapon(XiangYaoBaoZhang);
ArrayList<Person> persons = new ArrayList<Person>();
&nbs
补充:移动开发 , Android ,