23种设计模式——适配器模式

适配器模式(Adapter)

本质:转换匹配,复用功能

 两个已经成型的类A,B :

引入一个C类:

组合:

分类

  • 类的适配器采用多继承,带来了不良的高耦合,不推荐使用。
  • 对象的适配器采用对象组合,符合松耦合的原则

适用场景

希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。比如遗留代码复用、类库迁移。

注意:在设计阶段不要使用适配器模式,它不是为了解决开发阶段的问题,而是解决正在服役的项目问题。

优缺点

优点:

  • 可复用性
  • 可扩展性:实现适配器功能时,还可以开发其它功能,从而扩展系统的功能

缺点:

  • 过多地使用适配器,会让系统非常凌乱,不容易整体进行把握。

对象的适配器模式

在这里插入图片描述

客户端需要调用Request方法,而Adaptee没有该方法,于是增加一个类Adapter,连接客户端与Adaptee。

Adapter类继承Target类或者实现Target接口

Adapter类拥有一个Adaptee的实例

由于Adapter与Adaptee是合成聚合关系,所以是对象的适配器。

目标(Target)角色:

客户使用的接口,可以是具体类,抽象类,接口。

源(Adaptee)角色:

需要适配的类。

适配器(Adapter)角色:

继承Target类或者实现Target接口,在内部使用一个Adaptee对象,把源接口转换成目标接口。

public class Target {
	public void request() {
		System.out.println("普通请求");
	}
}
public class Adaptee {
	 public void SpecificRequest() {
		 System.out.println("特殊请求");
	 }
}
public class Adapter extends Target{
	private Adaptee adaptee = new Adaptee();

	@Override
	public void request() {
		adaptee.SpecificRequest();
	}
}
public class Main {
	public static void main(String[] args) {
		Target target = new Adapter();
		target.request();
	}
}

类的适配器模式

在这里插入图片描述

客户端需要调用Request方法,而Adaptee没有该方法,因此增加一个Adapter类。Adapter类实现了Target接口,并继承Adaptee。Adapter类的Request方法重新封装了Adaptee的SpecificRequest方法,实现了适配的目的。

目标(Target)角色:
客户使用的接口(在不支持多继承时,必须是接口)

源(Adaptee)角色:
需要适配的类。

适配器(Adapter)角色:
把源接口转换成目标接口

public interface ITarget {
	void request();
}

public class Adaptee {
	public void SpecificRequest() {
		 System.out.println("特殊请求");
	}
}

public class Adapter extends Adaptee implements ITarget{

	@Override
	public void request() {
		this.SpecificRequest();
	}	
}

public class Main {

	public static void main(String[] args) {
		ITarget t = new Adapter();
		t.request();
	}
}

实例——翻译

//教练类:需要被转换的类
public abstract class Player {
	protected String name;
	
	public Player(String name) {
		super();
		this.name = name;
	}
	public abstract void attack();
	public abstract void defense();

}
//中锋:听得懂教练说话
public class Center extends Player{
	public Center(String name) {
		super(name);
	}

	@Override
	public void attack() {
		System.out.println(name+":Attack!");
	}

	@Override
	public void defense() {
		System.out.println(name+":Defense!");
	}
	
}
//翻译类,要继承教练类,才能把教练类的话原原本本的翻译出来
public class Adapter extends Player{
	//需要适配的对象
	YaoMing ym;
	
	public Adapter(String name,YaoMing ym) {
		super(name);
		this.ym = ym;
	}

	@Override
	public void attack() {
		System.out.print(name+":");
		ym.attack();
	}

	@Override
	public void defense() {
		System.out.print(name+":");
		ym.defense();
	}

}
//需要适配的类
public class YaoMing {
	public void attack() {
		System.out.println("进攻!");
	}
	public void defense() {
		System.out.println("防守!");
	}
}
public class Main {

	public static void main(String[] args) {
		Player p1=new Center("中锋");
		p1.defense();
		YaoMing ym=new YaoMing();
		
		//听得懂中文
		Player p2=new Adapter("外籍球员",ym);
		p2.attack();
		p2.defense();	
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值