亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

利用多態(tài)性同時(shí)傳回多個(gè)結(jié)果和使用多種行為的方法
P粉187677012
P粉187677012 2023-09-06 00:04:47
0
1
732
<p>我正在使用 Laravel 9 並且我有一個(gè)請(qǐng)求可以包含:</p> <ul> <li>名為 SEASON 的參數(shù),值可以是 <code>array</code> 或 null 所以 <code>SEASON</code> 參數(shù)可以是 <code>array</code> 也可以是 <code>null</code></li> <li>名稱(chēng)為 EXPIRY 的參數(shù)可以是 <code>array</code> 也可以是 <code>null</code></li> </ul> <p>我有兩個(gè)類(lèi),一個(gè)用於<code>SEASON</code> 功能,另一個(gè)用於<code>EXPIRY</code> ,它們都從<code>Repository</code>擴(kuò)展。兩者都有一個(gè)名為 <code>execute</code> 的方法,該方法傳回一個(gè)陣列</p> <pre class="brush:php;toolbar:false;">abstract class Repository { abstract public function execute(): array; } class Expiry extends Repository { public function execute() { return ['The Request contain Expiry Parameter, and seasonal behaviours is done']; } } class Season extends Repository { public function execute() { return ['The Request contain Season Parameter, and expiry behaviours is done']; } }</pre> <p>如果我的請(qǐng)求包含SEASON,我想呼叫Season類(lèi)別的execute方法,或者如果我的請(qǐng)求包含Expiry,我想要呼叫expiry的execute方法。或者調(diào)用它們並將執(zhí)行的執(zhí)行返回合併到一個(gè)數(shù)組中,以便我可以得到結(jié)果。 </p> <pre class="brush:php;toolbar:false;">['The Request contain Expiry Parameter, and seasonal behaviours is done', 'The Request contain Expiry Parameter, and expiry behaviours is done'] Request contain Expiry Parameter, and expiry behaviours is done']>/pre&> ; <p>這就是我在控制器中嘗試過(guò)的:</p> <pre class="brush:php;toolbar:false;">public function bootstrap($data) { $parseTopics = Helper::parseTopicsRequest(); $basicProgram = new BasicProgramRepository(); $seasonalProgram = new SeasonalProgramRepository($parseTopics['SEASONAL']); $object = count($parseTopics['SEASONAL']) ? $seasonalProgram : $basicProgram; // Polymorphism return $object->execute(); }</pre> <p>問(wèn)題1: 我不確定我是否應(yīng)該使用這種方式或類(lèi)似的方式來(lái)解決我的需求:</p> <pre class="brush:php;toolbar:false;">$employe = new Program(new BasicProgramRepository());</pre> <p>預(yù)期結(jié)果: 預(yù)期結(jié)果取決於我是否有季節(jié)參數(shù)和到期時(shí)間。我想要實(shí)現(xiàn)的是使用不同的行為(execute方法)</p>
P粉187677012
P粉187677012

全部回覆(1)
P粉086993788

如果你想實(shí)作多型方法,最好建立儲(chǔ)存庫(kù)或僅用於管理該邏輯的東西。

這是範(fàn)例。

class SampleRepository
{
    /**
     * repository instance value
     *
     * @var string[] | null
     */
    private $sampleArray; // maybe here is SEASON or EXPIRY or null

    /**
     * constructor
     *
     * @param string[] | null $sampleArray
     */
    public function __construct($sampleArray)
    {
        $this->sampleArray = $sampleArray;
    }

    /**
     * execute like class interface role
     *
     * @return array
     */
    public function execute()
    {
        return (!$this->sampleArray) ? [] : $this->getResult();
    }

    /**
     * get result
     *
     * @return array
     */
    private function getResult()
    {
        // maybe pattern will be better to manage another class or trait.
        $pattern = [
            "SEASON" => new Season(),
            "EXPIRY" => new Expiry()
        ];
        return collect($this->sampleArray)->map(function($itemKey){
            $requestClass = data_get($pattern,$itemKey);
            if (!$requestClass){ // here is space you don't expect class or canIt find correct class
                return ["something wrong"];
            }
            return $requestClass->execute();
        })->flatten();
    }
}

你可以這樣呼叫。

$sampleRepository  = new SampleRepository($sampleValue); // expect string[] or null like ["SEASON"],["SEASON","EXPIRY"],null
    $result = $sampleRepository->execute(); // [string] or [string,string] or []

此方法僅適用於您的參數(shù)指定值。 如果Season類(lèi)別和Expiry類(lèi)別的回傳結(jié)果幾乎相同,那麼最好在trait上進(jìn)行管理。 (即範(fàn)例程式碼中的 $pattern)

嘗試一些。

我讀了評(píng)論,所以關(guān)注..

例如,它更願(yuàn)意只取得 getResult() 的結(jié)果。 因此,某些模式和如此多的邏輯不應(yīng)該寫(xiě)在 getResult() 上;

如果您使用特徵,這是一個(gè)範(fàn)例。 首先,您需要建立管理行為類(lèi)別。

行為.php

<?php 
namespace App\Repositories;

class Behavior
{
    use Behavior\BehaviorTrait;
    // if you need to add another pattern, you can add trait here.
}

然後,您需要在同級(jí)位置建立Behavior目錄。 你移動(dòng)該目錄,你就創(chuàng)建了這樣的特徵檔。

<?php

namespace App\Repositories\Behavior;

trait BehaviorTrait
{
    public static function findAccessibleClass(string $itemKey)
    {
      return data_get([
        "SEASON" => new Season(),
        "EXPIRY" => new Expiry()
      ],$itemKey);
    }
}

findAccessibleClass() 方法負(fù)責(zé)找出正確的類(lèi)別。

然後,你可以像這樣呼叫這個(gè)方法。

private function getResult()
    {
        return collect($this->sampleArray)->map(function($itemKey){
            $requestClass = Behavior::findAccessibleClass($itemKey); // fix here.
            if (!$requestClass){ // here is space you don't expect class or canIt find correct class
                return ["something wrong"];
            }
            return $requestClass->execute();
        })->flatten();
    }

如果 getResult() 中的程式碼太多,最好將負(fù)責(zé)的程式碼分開(kāi)。

要建立Behavior Trait,getResult不需要負(fù)責(zé)行為邏輯。簡(jiǎn)而言之,它將很容易測(cè)試或修復(fù)。

希望一切順利。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板