适配器模式(Adapter Pattern)-PHP实现

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。

  • 意图:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

  • 主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。

  • 何时使用: 1、系统需要使用现有的类,而此类的接口不符合系统的需要。 2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。 3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)

  • 如何解决:继承或依赖(推荐)。

  • 关键代码:适配器继承或依赖已有的对象,实现想要的目标接口。

  • 应用实例: 1、美国电器 110V,中国 220V,就要有一个适配器将 110V 转化为 220V。 2、JAVA JDK 1.1 提供了 Enumeration 接口,而在 1.2 中提供了 Iterator 接口,想要使用 1.2 的 JDK,则要将以前系统的 Enumeration 接口转化为 Iterator 接口,这时就需要适配器模式。 3、在 LINUX 上运行 WINDOWS 程序。 4、JAVA 中的 jdbc。

  • 优点: 1、可以让任何两个没有关联的类一起运行。 2、提高了类的复用。 3、增加了类的透明度。 4、灵活性好。

  • 缺点: 1、过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。

  • 使用场景:有动机地修改一个正常运行的系统的接口,这时应该考虑使用适配器模式。

  • 注意事项:适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。

示例代码:

<?php
interface Power{
	function passthrough();
}

// Adaptee
class ChinesePower implements Power{
	function passthrough(){
		echo 'Chinese Standard voltage:220V',PHP_EOL;
		echo 'Chinese Standard frequency:50HZ',PHP_EOL;
	}

	function getStandard(){
		return 'chinese';
	}
}

// Adaptee
class EuropeanPower implements Power{
	function passthrough(){
		echo 'European Standard voltage:240V',PHP_EOL;
		echo 'European Standard frequency:50HZ',PHP_EOL;
	}

	function getStandard(){
		return 'european';
	}
}

// Target
class AmericanPower implements Power{
	function passthrough(){
		echo 'American Standard voltage:120V',PHP_EOL;
		echo 'American Standard frequency:60HZ',PHP_EOL;
	}

	function getStandard(){
		return 'american';
	}
}

// Adapter fo American
class PowerAdapter implements Power{
	private $not_us_power;
	private $us_power;

	function __construct(Power $not_us_power){
		$this->not_us_power=$not_us_power;
		$this->us_power=new AmericanPower;
	}

	function passthrough(){
		$this->not_us_power->passthrough();
		echo 'adapter is transducing current ...',PHP_EOL;
		$this->us_power->passthrough();
	}

	function getStandard(){
		return 'american';
	}
}

// like Mac,Iphone etc
class AmericanProduct{
	function use($power){
		echo 'turn on electricity.',PHP_EOL;

		$power->passthrough();

		if ($power->getStandard()!='american') {
			echo 'not american standard power,product can not work.',PHP_EOL;
			return ;
		}

		echo 'product is working.',PHP_EOL;
	}
}

function main(){
	// demo
	
	$cn_power=new ChinesePower;
	$eu_power=new EuropeanPower;
	$us_power=new AmericanPower;

	//you can change reference parameter to be $eu_power
	$adapter_power=new PowerAdapter($eu_power);

	$product=new AmericanProduct;

	//you can change reference parameter to be $cn_power,$eu_power or $adapter_power
	$product->use($us_power);

	echo PHP_EOL;

	$product->use($adapter_power);

	echo PHP_EOL;

	$product->use($cn_power);

}main();

运行结果:

turn on electricity.
American Standard voltage:120V
American Standard frequency:60HZ
product is working.

turn on electricity.
European Standard voltage:240V
European Standard frequency:50HZ
adapter is transducing current ...
American Standard voltage:120V
American Standard frequency:60HZ
product is working.

turn on electricity.
Chinese Standard voltage:220V
Chinese Standard frequency:50HZ
not american standard power,product can not work.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值