定义
将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
结构图
image.png
-
Client:使用者,期望使用request()的方式来发送请求。 -
Target:使用者期望的接口(或者抽象类),里面有request(),所有符合使用者的处理者应该继承这个类。 -
Adapter:继承了Target的适配器,适配器中包含不符合规范需要适配的处理者,它request()调用不符合规范处理者的特殊接口。 -
Adaptee:不符合规范的处理者,它的接口不是request()但是做了同样的事情,通过Adapter适配器request()调用,才能为Client所用。
使用场景
-
当我们发现系统的数据和行为都正确,但接口不符合时,我们应该考虑用适配器,目的使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。
-
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。
NBA翻译的例子
- 结构图
image.png
- Player类(Target): 抽象基类,定义两个抽象方法
abstract class Player {
protected String name;
public Player(String name){
this.name = name;
}
// 返回当前动作,用于界面显示
protected String action;
protected Player() {
}
public String show() {
Log.v("Player", action);
return action;
}
// 进攻
public abstract void attack();
// 防守
public abstract void defence();
}
- Forward、Center、Guard等类,正常的具体的Target类,也就是那些不需要适配的类。
class Forward extends Player {
public Forward(String name) {
super(name);
}
@Override
public void attack() {
action = "前锋" + name + "进攻";
}
@Override
public void defence() {
action = "前锋" + name + "防守";
}
}
class Center extends Player {
public Center(String name) {
super(name);
}
@Override
public void attack() {
action = "中锋" + name + "进攻";
}
@Override
public void defence() {
action = "中锋" + name + "防守";
}
}
class Guard extends Player {
public Guard(String name) {
super(name);
}
@Override
public void attack() {
action = "后卫" + name + "进攻";
}
@Override
public void defence() {
action = "后卫" + name + "防守";
}
}
- ForeignCenter类,(Adaptee),不是Player(Target)的子类,但是具有相同行为的方法,方法名字不相同。
class ForeignCenter {
private String name;
private String action;
public void setName(String name) {
this.name = name;
}
public String getAction() {
return action;
}
public void 进攻() {
action = "外籍中锋" + name + "进攻";
}
public void 防守() {
action = "外籍中锋" + name + "防守";
}
}
- Translator类,(Adapter),适配器类,也是Player,(Target),的子类,里面有一个ForeignCenter的成员,进行”适配操作“
class Translator extends Player {
private ForeignCenter foreignCenter = new ForeignCenter();
public Translator(String name) {
this.name = name + "的翻译";
foreignCenter.setName(name);
}
@Override
public void attack() {
foreignCenter.进攻();
action = foreignCenter.getAction();
}
@Override
public void defence() {
foreignCenter.防守();
action = foreignCenter.getAction();
}
}
- 测试界面
image.png
- 客户端代码
public class AdapterActivity extends AppCompatActivity {
public static void launch(Context context) {
if (null != context) {
Intent intent = new Intent();
intent.setClass(context, AdapterActivity.class);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
}
}
CheckBox isForeignCheckBox;
EditText forwardEditText;
EditText centerEditText;
EditText guardEditText;
TextView actionTextView;
Player forward;
Player guard;
Player center;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adapter);
setTitle("适配器模式");
isForeignCheckBox = findViewById(R.id.checkBoxIsForeign);
forwardEditText = findViewById(R.id.editTextForward);
centerEditText = findViewById(R.id.editTextCenter);
guardEditText = findViewById(R.id.editTextGuard);
actionTextView = findViewById(R.id.textViewAction);
}
public void onAttackClick(View view) {
buildTeam();
forward.attack();
center.attack();
guard.attack();
String action = forward.show() + "\n"
+ center.show() + "\n"
+ guard.show();
actionTextView.setText(action);
}
public void onDefenceClick(View view) {
buildTeam();
forward.defence();
center.defence();
guard.defence();
String action = forward.show() + "\n"
+ center.show() + "\n"
+ guard.show();
actionTextView.setText(action);
}
private void buildTeam() {
forward = new Forward(forwardEditText.getText().toString());
guard = new Guard(guardEditText.getText().toString());
if (isForeignCheckBox.isChecked()) {
center = new Translator(centerEditText.getText().toString());
} else {
center = new Center(centerEditText.getText().toString());
}
}
}
Demo地址
https://gitee.com/zhangxusong888/Android/tree/master/design_pattern









网友评论