就是單純的討論一下程式碼

Remember發表於2019-10-17

手把手使用PHP實現LRU快取淘汰演算法

:pencil2:前言

開發時間短,程式碼寫的也少,所有格外的想了解別人是咋麼寫程式碼了,細節到想了解變數的宣告,單純的說探討是說不出什麼的,所以接下來我從一個日常開發的業務中開始說起,以我現有的程式碼說起。

:pencil2:業務場景以及個人程式碼實現

比如在一些OA等系統中必然存在申請之類的流程,請假是申請,報銷是申請....既然有申請,當然就會涉及到轉單,稽核或者抄送之類的流程工作,我不知道你們平常咋麼寫程式碼了,我就說說我的。

假設現在有3鐘不同型別的訂單,操作著各自不同的表,可能不僅僅對應著一張表,對應流程的操作都大體相同,只是對應操作之後的業務有自己後續的處理。如果是我,第一步是把公共的稽核,申請......這些公共的部分抽離出來

<?php

namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;

abstract class BaseOrderController extends Controller
{
    //yes or no
    protected $agree = 1;
    protected $refuse = 2;

    //稽核狀態
    protected $handle_no = 0; 
    protected $handle_agree = 1; 
    protected $handle_refuse = 2;
    protected $handle_doing = 3; 

 protected $hasOwnTaking = false;

    abstract function getModel();

    abstract function getChangeModel();

    abstract function getHandleModel();

    abstract function getTable();

   abstract function extraHandle(Order $order);

    public function extraTaking(Order $order)
    {
       return true;
    }

    public function ownResponse($order)
    {
        return new BaseOrderResource($order);
    }

    /**
     * @param $id
     * @return BaseOrderResource
     * 公共方法
     */
    public function show($id)
    {
        //其他公共業務程式碼
    }

    /**
     * @param Request $request
     * @return WithdrawResource|mixed
     * @throws InternalException
     * 轉單
     */
    public function change(Request $request)
    {
       //公共的轉單
    }

    /**
     * @param Request $request
     * @return WithdrawResource
     * @throws InternalException
     * 公共
     */
    public function isTaking(Request $request)
    {
                //some code
                if(true===$this->hasOwnTaking){
                    $this->extraTaking($xxx);
                }
    }

    /**
     * @param Request $request
     * @return BaseOrderResource
     * @throws InternalException
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * 公共
     */
    public function handle(Request $request)
    {
         //some code 
         $this->extraHandle($xxx);
    }

}

然後三鐘不同的申請

<?php

namespace App\Http\Controllers\Branch;

use App\Http\Controllers\Common\BaseOrderController;
use 其他的

class DepositOrderController extends BaseOrderController
{
    public function getModel()
    {
        return 'App\Models\DepositOrder';
    }

    public function getChangeModel()
    {
        return 'App\Models\DepositChange';
    }

    public function getHandleModel()
    {
        return 'App\Models\DepositHandle';
    }

    public function getTable()
    {
        return 'deposit_orders';
    }

    public function extraHandle(object $order)
    {
       //自己的業務邏輯程式碼
       //所有自己的的流程操作完畢,需要執行額外的服務,但是對應的裡面的細節也不相同
        $merchantService = new MerchantService(new SubMerchantSomething());
        $merchantService->make($order, $order->amount);
    }

    //子類如需要自己響應
    public function ownResponse($order)
    {
        return new DepositOrderShow($order);
    }

   //真實當然不會這麼起 還想多活兩年
    public function aaa()
    {
        //自己的code
    }

    public function bbb()
    {
        //自己的code
    }

}

第二個類

<?php

namespace App\Http\Controllers\Branch;

use App\Http\Controllers\Common\BaseOrderController;
use 其他;

class WithdrawOrderController extends BaseOrderController
{
  protected $hasOwnTaking = true;
    public function getTable()
    {
        return 'withdraw_orders';
    }

    public function getModel()
    {
        return 'App\Models\WithdrawOrder';
    }

    public function getChangeModel()
    {
        return 'App\Models\WithdrawChange';
    }

        public function getHandleModel()
    {
        return 'App\Models\WithdrawHandle';
    }

    public function extraTaking(object $order)
    {
        //處理自己的。。。
    }

    public function extraHandle(object $order)
    {
       //處理自己的額外稽核
       // some code 

        $merchantService = new MerchantService(new AddMerchantSomething());

        $merchantService->make($order, 引數a, 引數b);
    }

    public function someCode()
    {
      ///自己的東西
    }

   public function someCode()
    {
      ///自己的東西
    }

    /**
     * @param $order
     * @return \App\Http\Resources\Common\BaseOrderResource|WithdrawDetail
     * 子類自己想要的響應格式
     */
    public function ownResponse($order)
    {
        return new WithdrawDetail($order);
    }

}

第三個

<?php

namespace App\Http\Controllers\Branch;
use App\Http\Controllers\Common\BaseOrderController;
use 其他的;

class PaymentController extends BaseOrderController
{

    protected $hasOwnTaking = true;

    public function getModel()
    {
        return 'App\Models\PaymentOrder';
    }

    public function getChangeModel()
    {
        return 'App\Models\PaymentChange';
    }

    public function getTable()
    {
        return 'payment_order';
    }

    public function getHandleModel()
    {
       //some code for this
    }

    public function show($id)
    {
     $parent=parent::show($id);
       //自己的code
    }

    public function extraHandle()
    {
       //some code
    }

}

所有各子類中對於額外的處理服務又使用策略模式進一步封裝

<?php

namespace App\Service;

class MerchantService
{
    protected $changeMerchant; 

    public function __construct(ChangeMerchantInterface $ChangeMerchant)
    {
        $this->changeMerchant = $ChangeMerchant;
    }

    /**
     * @param Merchant $merchant
     * @param $attribute1
     * @param $attribute2
     */
    public function make(Merchant $merchant, $attribute1, $attribute2=0)
    {
     //業務邏輯 code
    $this->changeMerchant->changeMerchant($merchant,$attribute1,$attribute2);
    }
}

接著實現一個公共介面

<?php

namespace App\Service;
use Merchant;
Interface ChangeMerchantInterface
{

    /**
     * @param Merchant $merchant 
     * @param $attribute1    
     * @param $attribute2 
     * @return mixed
     *   
     */
    public function changeMerchant(Merchant $merchant, $attribute1, $attribute2);
}
<?php

namespace App\Service;

class AddMerchantSomething implements ChangeMerchantInterface
{
    public function changeMerchant($merchant, $attribute1, $attribute2)
    {
      //一些在這個類中做的操作
    }
}
<?php

namespace App\Service;

class SubMerchantSomething implements ChangeMerchantInterface
{
    public function changeMerchant($merchant, $attribute1, $attribute2)
    {
        //在這個類中乾的事情
    }
}

那麼本篇文章就到此結束了,以上是我個人的做法,當然也去掉了不忍直視的業務程式碼,就是不知道你們平常咋麼寫程式碼的。

吳親庫裡

相關文章