相对来说比较简单,就是为了让不符合原来系统里的接口 让他符合标准,所以要单独写一个类来调用这个类,以便符合原来系统里的接口
Target:符合的标准,写成接口,不同的apater可以implement它。
Adapter:适配器(操作其它的类以符合标准)
Adaptee: 需要适配的类
ITarget interface
interface ITarget
{
public function commonMethod();
}
Adapter
class Adapter implements ITarget
{
private $adaptee;
public function __construct(Adaptee $adaptee)
{
$this->adaptee = $adaptee;
}
public function commonMethod()
{
return $this->adaptee->ownMethod();
}
}```
Adaptee
```php
class Adaptee
{
public function ownMethod()
{
echo "This is the Adaptee method";
}
}
使用:
$apdatee = new Adaptee();
$client = new Adapter($apdatee);
$client->commonMethod();
如果有个其它的需要适配的,可以再写一套,apatee 和 adapter。
一般用于不想修改原始和新的接口的时候 可以使用。