IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> php-适配器模式实现 -> 正文阅读

[PHP知识库]php-适配器模式实现

php-适配器模式实现

概述

适配器模式(Adapter) :将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。适配器模式既可以作为类结构型模式,也可以作为对象结构型模式。

我们这里还是以摩托车制造为例。客户要求生产蓝色和红色的踏板摩托车,除颜色外其他属性都一样。我们先前的工艺是把车身喷漆部分单独视作一个流程,现在考虑到流程优化尝试,我们想把车身喷漆这一流程合并到车身组装环节。但并不想破坏原来的流程,所以我们需要定制两套车身组装标准(新标准基于原有标准的拓展)。此时我们就需要利用适配器模式来实现。具体代码实现如下。

模式结构

类结构型模式

  1. MotorcycleProduce-摩托组装抽象类:建立摩托组装标准工艺 Target 目标类
  2. MotocycleProduct 摩托车产品本身
  3. motorcycleScooter 摩托车组装- Adapter 适配器类
  4. motorcycleScooterAdapter 摩托车组装-Adaptee 适配者类

对象结构型模式

  1. MotorcycleProduce-摩托组装抽象类:建立摩托组装标准工艺 Target 目标类
  2. MotocycleProduct 摩托车产品本身
  3. motorcycleScooter 摩托车组装- Adapter 适配器类
  4. motorcycleScooterAdapter 摩托车组装-Adaptee 适配者类

UML图例

类结构型模式

在这里插入图片描述

对象结构型模式

在这里插入图片描述

代码实例

类结构型模式

<?php 
namespace Adapter;
//抽象类-定义生产摩托车流水线标准  Target 目标类
interface  MotorcycleProduce 
{
    //发动机方法
    public function addEngine();
    //车身方法
    public function addBody();
    public function addBodyAndSetColor(string $color);
    //车轮方法
    public function addWhell();
    //喷漆方法
    public function setBodyColor(string $color);
    //获取摩托
    public function getMotor();
}
//摩托车产品本身
class MotocycleProduct{
    private $motor = [
        "engine"=>"",
        "body"=>"",
        "whell"=>"",
        "bodyColor"=>"blue"
    ];
    //新增发动机零部件
    public function addEngine($engine){
        $this->motor["engine"] = $engine;
    }
    public function addBody($body){
        $this->motor["body"] = $body;
    }
    public function addWhell($whell){
        $this->motor["whell"] = $whell;
    }
    public function setBodyColor(string $color){
        $this->motor["bodyColor"] = $color;
    }
    //获取完整摩托对象
    public function getMotor(){
        return $this->motor;
    }
}
/**
 * 踏板摩托组装  Adapter 适配器类
 */
class motorcycleScooter
{
    protected $motor;
    public function __construct()
    {
        $this->motor = new MotocycleProduct();
    }
    public function addEngine()
    {
        $this->motor->addEngine("踏板摩托-发动机已装好");
    }
    public function addBody(){
        $this->motor->addBody("踏板摩托-车身已装好");
    }
    public function addWhell(){
        $this->motor->addWhell("踏板摩托-车轮已装好");
    }
    public function setBodyColor($color){
        $this->motor->setBodyColor($color);
    }
    public function getMotor(){
        return $this->motor;
    }
}
/**
 * motorcycleScooterAdapter Adaptee 适配者类
 */
class motorcycleScooterAdapter extends motorcycleScooter implements MotorcycleProduce{
    public function addBodyAndSetColor($color){
        $this->addBody();
        $this->setBodyColor("green");
    }
}
$motorcycleScooter = new motorcycleScooterAdapter;
$motorcycleScooter->addEngine();
$motorcycleScooter->addBody();
$motorcycleScooter->addWhell();
$motorcycleScooter->setBodyColor("red");
$motor = $motorcycleScooter->getMotor();
var_dump($motor);
echo "<br>";
$motorcycleScooter = new motorcycleScooterAdapter;
$motorcycleScooter->addEngine();
$motorcycleScooter->addBodyAndSetColor("green");
$motorcycleScooter->addWhell();
$motor = $motorcycleScooter->getMotor();
var_dump($motor);

对象结构型模式

<?php 
namespace AdapterObj;
//抽象类-定义生产摩托车流水线标准  Target 目标类
interface  MotorcycleProduce 
{
    //发动机方法
    public function addEngine();
    //车身方法
    public function addBody();
    //组装车身并设置颜色
    public function addBodyAndSetColor(string $color);
    //车轮方法
    public function addWhell();
    //喷漆方法
    public function setBodyColor(string $color);
    //获取摩托
    public function getMotor();
}
//摩托车产品本身
class MotocycleProduct{
    private $motor = [
        "engine"=>"",
        "body"=>"",
        "whell"=>"",
        "bodyColor"=>"blue"
    ];
    //新增发动机零部件
    public function addEngine($engine){
        $this->motor["engine"] = $engine;
    }
    public function addBody($body){
        $this->motor["body"] = $body;
    }
    public function addWhell($whell){
        $this->motor["whell"] = $whell;
    }
    public function setBodyColor(string $color){
        $this->motor["bodyColor"] = $color;
    }
    //获取完整摩托对象
    public function getMotor(){
        return $this->motor;
    }
}
/**
 * 踏板摩托组装  Adapter 适配器类
 */
class motorcycleScooter
{
    protected $motor;
    public function __construct()
    {
        $this->motor = new MotocycleProduct();
    }
    public function addEngine()
    {
        $this->motor->addEngine("踏板摩托-发动机已装好");
    }
    public function addBody(){
        $this->motor->addBody("踏板摩托-车身已装好");
    }
    public function addWhell(){
        $this->motor->addWhell("踏板摩托-车轮已装好");
    }
    public function setBodyColor($color){
        $this->motor->setBodyColor($color);
    }
    public function getMotor(){
        return $this->motor;
    }
}
/**
 * motorcycleScooterAdapter Adaptee 适配者类
 */
class motorcycleScooterAdapter implements MotorcycleProduce{
    private $motorcycleScooter;
    public function __construct($motorcycleScooter){
        $this->motorcycleScooter = $motorcycleScooter;
    }
    public function addBodyAndSetColor($color){
        $this->motorcycleScooter->addBody();
        $this->motorcycleScooter->setBodyColor($color);
    }
    public function addBody(){
        $this->motorcycleScooter->addBody();
    }
    public function addEngine(){
        $this->motorcycleScooter->addEngine();
    }
    public function addWhell(){
        $this->motorcycleScooter->addWhell();
    }
    public function setBodyColor(string $color){
        $this->motorcycleScooter->setBodyColor($color);
    }
    public function getMotor(){
        return $this->motorcycleScooter->getMotor();
    }
}
$motorcycleScooter = new motorcycleScooterAdapter(new motorcycleScooter);
$motorcycleScooter->addEngine();
$motorcycleScooter->addBody();
$motorcycleScooter->addWhell();
$motorcycleScooter->setBodyColor("red");
$motor = $motorcycleScooter->getMotor();
var_dump($motor);
echo "<br>";
$motorcycleScooter = new motorcycleScooterAdapter(new motorcycleScooter);
$motorcycleScooter->addEngine();
$motorcycleScooter->addBodyAndSetColor("green");
$motorcycleScooter->addWhell();
$motor = $motorcycleScooter->getMotor();
var_dump($motor);

模式分析

适配器模式(Adapter)通常适用于以下场景。

  • 以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致。
  • 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-13 11:42:08  更:2021-08-13 11:44:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 10:11:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码