laravel 依赖注入 接口设计

 

假设我如今须要作一个支付服务,那么我先设计一个接口html

interface PayInterface{
    public function pay(Order $order) : string;
}

而后实现这个接口数据库

class WeiXinPay implements PayInterface{
    public function pay(Order $order) :string{
        //具体实现
    }
}

开始发现一个问题
微信支付是须要三个关键参数的 (appID , appSecret , key)
我就接着修改代码,我但愿这三个参数是经过外部注入的,而不是写死在WeiXinPay里面,因而乎我就修改了WeiXinPay的构造函数,并新增了一个接口与实现json

interface PaySettingsInterface {
    public function getSettings() : array;
}

class WeixinPaySettings implements PaySettingsInterface{
    public function getSettings() : array{
        return [
            'app_id' => 'xxxx',
            'app_secret' => 'yyyyy',
            'key' => 'zzzz'
        ];
    }
}

class WeiXinPay implements PayInterface{

    protected $settings;
    public __construct(PaySettingsInterface $settings){
        $this->settings = $settings->getSettings();
    }

    public function pay(Order $order) :string{
        //具体实现
    }
}

好了。感受到这里这个PayInterface的实现应该是没问题了。我开始写服务提供者缓存

$this->app->bind(PaySettingsInterface::class,WeiXinPaySettings::class);
    $this->app->bind(PayInterface::class , WeiXinPay::class);

写一段测试代码来跑跑看微信

public function testPay(){
        $orderSn = Strings::randomString('NUMBER+',12);
        $order = factory(Order::class)->make([
            'sn' => $orderSn,
            'uid' => 111,
            'to_uid' => 109,
            'relation_user' => json_encode([109,108,107,104,102,12]),
            'amount' => 1,
            'attach' => 1,
            'status' => Constans::ORDER_NO_PAY,
            'is_settle' => Constans::NO_SETTLE,
        ]);
        
        /**
         * @var $service PayInterface
        */
        $service = $this->app->make(PayInterface::class);
        
        $res = $service->pay($order);
        $this->assertIsString($res);
    }

没有问题,一切都如预期同样。(将来我也能够很容置的将微信支付换成支付宝,只须要在服务提供者切换实现便可)app

过了两天,又有一个新的需求了。终极问题来了,老板但愿每一次支付的时候收款人都不同,也就是说微信支付的appId,appSecret,appKey每次都不同dom

我开始修改个人代码,我想着:我让这些有变更的参数经过构造函数的方式传递进来总能够吧。函数

interface PaySettingsInterface {
    public function getSettings() : array;
}

class WeixinPaySettings implements PaySettingsInterface{
    protected $appID;
    protected $appKey;
    protected $appSecret;

    public function __construct($appID ,$appKey ,$appSecret){
        $this->appID = $appID;
        $this->appKey = $appKey;
        $this->appSecret = $appSecret;
    }

    public function getSettings() : array{
        return [
            'app_id' => $this->appID,
            'app_secret' => $this->appSecret,
            'key' => $this->appKey
        ];
    }
}

class WeiXinPay implements PayInterface{

    protected $settings;
    public __construct(PaySettingsInterface $settings){
        $this->settings = $settings->getSettings();
    }

    public function pay(Order $order) :string{
        //具体实现
    }
}

//而后我修改个人服务提供者
$this->app->bind(PaySettingsInterface::class,function($app){
    //怎么new 呢? 老板的需求是可能每次都不一样,这些数据又可能来自数据库,也可能来自缓存。
    $instance = new WeixinPaySettings(???);
    return $instance;
});
$this->app->bind(PayInterface::class , WeiXinPay::class);
//到这里,看来是没法经过容器自动注入PaySettingsInterface的实现了。那么我就只能这样了。在测试代码中:

public function testPay(){
    $orderSn = Strings::randomString('NUMBER+',12);
    $order = factory(Order::class)->make([
        'sn' => $orderSn,
        'uid' => 111,
        'to_uid' => 109,
        'relation_user' => json_encode([109,108,107,104,102,12]),
        'amount' => 1,
        'attach' => 1,
        'status' => Constans::ORDER_NO_PAY,
        'is_settle' => Constans::NO_SETTLE,
    ]);
    
    //查询数据库获得settings
    $settings = Db::get();
    $paySettings  = new WeixinPaySettings($settings['app_id'],$settings['app_secret'],$settings['app_key']);
    
    $payService = new WeixinPay($paySettings);
    
    $res = $payService->pay($order);
    $this->assertIsString($res);
}

这样看起来也能够,可是我困扰了测试

  1. 我没有办法简单的替换支付方式了 (WeixinPay 替换成 AliPay)
  2. 调用方手动的去new 相关的实现,产生了严重的依赖。

我但愿可以:微信支付

  1. 可以简单的替换支付方式(服务提供者)
  2. 调用方只须要 调用 pay(Order $order) 方法便可完成支付,即便我切换支付对于调用方来讲都是不须要改变的,符合里氏替换规则
相关文章
相关标签/搜索