Android[高级教程] 设计模式之四 适配器模式
上一篇:http://www.zzzyk.com/kf/201202/121055.html
这次我们主要来介绍适配器模式,适配器模式主要是根据传入的对象类型来匹配使用的方法,这里还是以《西游记》为主题,详细介绍一下唐僧每个徒弟使用的武器,孙悟空用"金箍棒",八戒用"九齿钉耙",沙僧用"降妖宝仗",每个人使用的武器都不一样,这样就通过适配器的方法来适配每个人使用的武器。好了,接下来我们看代码,首先定义了一个Person接口,实现了取得名字的方法:
[java] public inte易做图ce Person {
public String getName();
}
public inte易做图ce Person {
public String getName();
}
然后就是三个徒弟类了:
[java] public class Wukong implements Person {
@Override
public String getName() {
return "孙悟空";
}
}
public class Wukong implements Person {
@Override
public String getName() {
return "孙悟空";
}
}[java] public class Bajie implements Person {
@Override
public String getName() {
return "八戒";
}
}
public class Bajie implements Person {
@Override
public String getName() {
return "八戒";
}
}[java] public class Shaseng implements Person {
@Override
public String getName() {
return "沙僧";
}
}
public class Shaseng implements Person {
@Override
public String getName() {
return "沙僧";
}
}好,定义好,接下来就是武器类了:
[java] public class Weapon {
public Weapon() {
}
public String getWeapon(Wukong wukong) {
return wukong.getName() + "使用金箍棒";
}
public String getWeapon(Bajie bajie) {
return bajie.getName() + "使用九齿钉耙";
}
public String getWeapon(Shaseng shaseng) {
return shaseng.getName() + "使用降妖宝仗";
}
}
public class Weapon {
public Weapon() {
}
public String getWeapon(Wukong wukong) {
return wukong.getName() + "使用金箍棒";
}
public String getWeapon(Bajie bajie) {
return bajie.getName() + "使用九齿钉耙";
}
public String getWeapon(Shaseng shaseng) {
return shaseng.getName() + "使用降妖宝仗";
}
}这里面就对调用的每个对象分别进行了区分。最后就是调用类了:
[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);
Weapon weapon = new Weapon();
ArrayList<String> lists = new ArrayList<String>();
lists.add(weapon.getWeapon(new Wukong()));
lists.add(weapon.getWeapon(new Bajie()));
lists.add(weapon.getWeapon(new Shaseng()));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, lists);
listView.setAdapter(adapter);
}
}
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);
Weapon weapon = new Weapon();
ArrayList<String> lists = new ArrayList<String>();
lists.add(weapon.getWeapon(new Wukong()));
lists.add(weapon.getWeapon(new Bajie()));
lists.add(weapon.getWeapon(new Shaseng()));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, lists);
listView.setAdapter(adapter);
}
}OK,最后看一下执行的效果呢,
好,接下来,我们对武器类进行重构呢,将传入的对象进行抽象化,通过判断每个传入类的类型来区别武器
[java] public class Weapon {
public Weapon() {
}
public String getWeapon(Person p) {
String weapon = null;
if (p.getClass() == Wukong.class) {
weapon = "使用金箍棒";
} else if (p.getClass() == Bajie.class) {
weapon = "使用九齿钉耙";
} else if (p.getClass() == Shaseng.class) {
weapon = "使用降妖宝仗";
}
return p.getName() + weapon;
}
}
public class Weapon {
public Weapon() {
}
public String getWeapon(Person p) {
String weapon = null;
if (p.getClass() == Wukong.class) {
weapon = "使用金箍棒";
} else if (p.getClass() == Baj
补充:移动开发 , Android ,