设计模式-- 工厂模式

最近重构 Ajax请求的代码,由于系统模块较多 且大部分结构一样      定义如下接口   

public interface IAjax
{

    public function init($data); 初始化 请求

    //本地 ajax
    public  function doAjax();

    //远程调用 ajax 执行
    public function doRpcAction();
}

矩阵模块 Ajax 处理

class MatrixAjax implements IAjax{

    public function init($data)
    {
        // TODO: Implement init() method.
    }

    public function doAjax()
    {
        // TODO: Implement doAjax() method.
    }

    public function doRpcAction()
    {
        // TODO: Implement doRpcAction() method.
    }
}

离线模块 请求ajax处理

class OfflineAjax implements IAjax{

    public function init($data)
    {
        // TODO: Implement init() method.
    }

    public function doAjax()
    {
        // TODO: Implement doAjax() method.
    }

    public function doRpcAction()
    {
        // TODO: Implement doRpcAction() method.
    }
}

本地 Api

class Api
{

    private $api;

    public function __construct()
    {

    }

    /**
     * 发送 ajax 请求数据
     * @param string $products 产品模块   系统| 离线| 矩阵| 编码器| 多画面| 字幕| 导播
     * @param array $data 数据
     * @param false $sync 是否异步发送信息
     */
    public  function sendMessage(string $products ,array $data,bool $sync = false){
        switch ($products) {
            case 'matrix':
                $this->api = new MatrixAjax($data);
                break;
            case 'offline':
                $this->api = new OfflineAjax($data);
                break;
            
            default:
        }

        if(isset($data['rpc_ip']) && $data['rpc_ip']){
           $this->api?->doRpcAction();

        }else{
            $this->api?->doAjax();
        }

    }
}

?>

Api 工厂

<?php
/**
 * @name clsFactoryApi
 * @author yanyl
 * @time 2022-07-14
 * API工厂  本地 api Ajax 以及 rpc api
 */
class clsFactoryApi
{

    /**
     * @var clsRpcApi|Api
     *
     */
    private clsRpcApi|Api $requestApi;

    /**
     *
     * @param $requestType string 请求类型 api | rpc api
     */
    public function __construct(private string $requestType){

         $this->createApi();
    }

    /**
     * @return void
     */
    private function createApi(): void
    {
        $this->requestApi =  $this->requestType == 'api' ? new Api() : new clsRpcApi();
    }


    /***
     * 请求处理
     * @param string $products
     * @param array $data
     */
    public function handle(string $products = '', array $data = []){

        $this->requestApi?->sendMessage($products,$data);
    }

}

远程 clsApiRpc

<?php
/**
 * @name clsRpcApi
 * @author yanyl
 * @time 2022-07-14
 * @description rpc 入口
 */
class clsRpcApi implements ISend
{
    private $rpcApi ;

    public function __construct()
    {

    }

    /**
     * @param string $products
     * @param array $data
     * @param bool $sync
     * @return bool|void
     */
    public function sendMessage(string $products  ,array $data,bool $sync = false){

        if(!$sync){
            $param = [
                'product'=>$products,
                'data' =>$data
            ];
            (new  Receive($this,$param))->handle();
            return true;
        }

        $this->createApi($products,$data);

        $this->rpcApi?->init();
    }

    /**
     * @param $type
     * @param $data
     */
    private function createApi($type,$data){

        switch ($type){
            case 'matrix':
                $this->rpcApi =  new clsMatrixRpc($data);
                break;
            case 'encoder_input':
                $this->rpcApi = new clsIngestRpc($data);
                break;
            case 'system':
                $this->rpcApi = new clsSystemRpc($data);
                break;
            case 'transcode':
                $this->rpcApi =  new clsTranscodeRpc($data);
                break;
            default:
        }
    }
}

 PHP 使用

$param = strtoupper($_SERVER['REQUEST_METHOD']) == 'GET' ? $_GET : $_POST;
(new clsFactoryApi('api'))->handle('offline',$param);

(new clsFactoryApi('rpc_api'))->handle('offline',$param);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值