@Chiang
2019-12-30T16:09:29.000000Z
字数 2768
阅读 605
设计模式
适配器模式是将类或者对象结合在一起形成复杂的、功能更为强大的结构
用适配器模式来包装其他一些不兼容的类或对象
- 支付
- 数据库
- 复杂业务逻辑接口使用适配器接口对外提供数据
接口:
<?php
/**
* 接口
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
interface DatabaseTarget
{
public function set();
public function get();
}
适配者类
<?php
/**
* 适配者类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
class DatabaseAdaptee
{
protected $driver = "mysql";
public function set()
{
echo "use {$this->driver}.\n";
}
}
适配器类 继承适配者类实现接口
<?php
/**
* 适配器类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
class DatabaseAdapter extends DatabaseAdaptee implements DatabaseTarget
{
public function __construct()
{
$this->driver = "pdo";
}
public function get()
{
echo "used {$this->driver}.\n";
}
}
客户类
<?php
/**
* 客户类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
require __DIR__ . '/../../vendor/autoload.php';
class Client
{
public static function run()
{
// 适配者
$adaptee = new DatabaseAdaptee();
$adaptee->set();
// 适配器
$adapter = new DatabaseAdapter();
$adapter->set();
$adapter->get();
}
}
Client::run();
接口
<?php
/**
* 接口
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
interface DatabaseTarget
{
public function set();
public function get();
}
适配者类
<?php
/**
* 适配者类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
class DatabaseAdaptee
{
public $driver = "mysql";
public function set()
{
echo "use {$this->driver}.\n";
}
}
适配器类 实现接口
<?php
/**
* 适配器类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
class DatabaseAdapter implements DatabaseTarget
{
protected $adaptee;
public function __construct(DatabaseAdaptee $adaptee)
{
$this->adaptee = $adaptee;
$adaptee->driver = "mysqli";
}
public function set()
{
echo "use {$this->adaptee->driver}.\n";
}
public function get()
{
echo "used {$this->adaptee->driver}.\n";
}
}
客户类 对象注入
<?php
/**
* 客户类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/1
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
require __DIR__ . '/../../vendor/autoload.php';
class Client
{
public static function run()
{
$adaptee = new DatabaseAdaptee();
$adaptee->set();
// 适配器
$adapter = new DatabaseAdapter($adaptee);
$adapter->set();
$adapter->get();
}
}
Client::run();