Home > Backend Development > PHP Tutorial > PHP设计模式之适配器模式

PHP设计模式之适配器模式

WBOY
Release: 2016-06-23 13:05:57
Original
942 people have browsed it

适配器模式属于结构型模式

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

使用前提:

适配器中主要角色

目标(Target)角色:定义客户端使用的与特定领域相关的接口,这也就是我们所希望得到的

源(Adaptee)角色:需要进行适配的接口

适配器(Adapter)角色:対Adaptee的接口与Target的接口进行适配;适配器是本模式的核心,适配器把源接口转换成目标接口,此角色为具体类

适配器使用场景

1、你想使用一个已经存在的类,而它的接口不符合你的需求

2、你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作

3、你想使用一个已经存在的子类,但是不可能对每一个都进行子类化以匹配他们的接口,对象适配器可以适配他的父类接口(仅限于对象适配器)

个人举例:

1 姚明在NBA打球,但是不懂英语,需要一个翻译进行翻译之后才能懂得战术

2 苹果手机使用安卓手机的充电线

namespace haibao\design\web\view\design;

use haibao\design\web\common\design\adapter\Forwards;

use haibao\design\web\common\design\adapter\Center;

use haibao\design\web\common\design\adapter\Translator;

class Adapter extends \haibao\design\web\view\Base{

protected function preRender(){

header("Content-type: text/html; charset=utf-8");

$forwards = new Forwards('科比');

$forwards->attack();

$forwards->defense();

echo "


";

$center = new Center('霍华德');

$center->attack();

$center->defense();

echo "


";

$translator = new Translator('姚明');

$translator->attack();

$translator->defense();

echo "


";

}

}

/**

* 中锋

*/

namespace haibao\design\web\common\design\adapter;

class Center extends Player{

public function __construct($name){

parent::__construct($name);

}

public function attack(){

echo '中锋'.$this->name.'进攻
';

}

public function defense(){

echo '中锋'.$this->name.'防守
';

}

}

/**

* 外籍前锋

*/

namespace haibao\design\web\common\design\adapter;

class ForeignCenter{

private $name;

public function __construct($name){

$this->name = $name;

}

public function chinaattack(){

echo '外籍前锋'.$this->name.'进攻
';

}

public function chinadefense(){

echo '外籍前锋'.$this->name.'防守
';

}

}

/**

* 前锋

*/

namespace haibao\design\web\common\design\adapter;

class Forwards extends Player{

public function __construct($name){

parent::__construct($name);

}

public function attack(){

echo '前锋'.$this->name.'进攻
';

}

public function defense(){

echo '前锋'.$this->name.'防守
';

}

}

/**

* 球员

*/

namespace haibao\design\web\common\design\adapter;

abstract class Player{

public $name;

public function __construct($name){

$this->name = $name;

}

public function attack(){

}

public function defense(){

}

}

/**

* 外籍中锋翻译员

*/

namespace haibao\design\web\common\design\adapter;

class Translator extends Player{

public $foreignCenter;

public function __construct($name){

$this->foreignCenter = new ForeignCenter($name);

}

public function attack(){

$this->foreignCenter->chinaattack();

}

public function defense(){

$this->foreignCenter->chinadefense();

}

}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template