TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:software-architecture:design-patterns:strategy

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:software-architecture:design-patterns:strategy [2024/08/12 16:50] tungntdevelopment:software-architecture:design-patterns:strategy [2024/08/19 09:41] (current) tungnt
Line 1: Line 1:
 ====== Strategy ====== ====== Strategy ======
 +
 +Strategy là một mẫu thiết kế hành vi cho phép xác định một họ thuật toán, đưa từng thuật toán vào một lớp riêng biệt thay vì phải dùng nhiều điều kiện if trong một function - khó tiếp cận và bảo trì.
 +
 +**Ví dụ khi chưa dùng Strategy, đoạn code quá nhiều if dẫn đến khó kiểm soát:**
 +
 +<file php>
 +function getProviderTransfer($userInfo, $bankInfo = null, $accountNo = null, $options = [])
 +{
 +    if(!empty($accountNo) && preg_match('/^(M999)/', $accountNo, $matches) && $bankInfo->code == Bank::BANK_CODE_TECHCOMBANK){
 +        return UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK;
 +    }
 +    if (!empty($accountNo) && preg_match('/^(966699)/', $accountNo, $matches) && $bankInfo->code == Bank::BANK_CODE_MSB) {
 +        return UserTransaction::PROVIDER_TRANSFER_VPBANK;
 +    }
 +    if (!empty($options['amount']) && is_numeric($options['amount']) && $options['amount'] < 2000) {
 +        if ($bankInfo->code == Bank::BANK_CODE_TECHCOMBANK  && !Bank::isAccountNoTCB($accountNo)) {
 +            return UserTransaction::PROVIDER_TRANSFER_VIETINBANK;
 +        }
 +
 +        if (in_array($bankInfo->code, [
 +            UserTransaction::PROVIDER_TRANSFER_TPBANK, UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK,
 +            UserTransaction::PROVIDER_TRANSFER_VIETINBANK, UserTransaction::PROVIDER_TRANSFER_MBBANK,
 +            UserTransaction::PROVIDER_TRANSFER_VPBANK])) {
 +                return $bankInfo->code;
 +        }
 +        return UserTransaction::PROVIDER_TRANSFER_VIETINBANK;
 +    }
 +
 +    $appSetting = AppSetting::get()->pluck('content','key')->toArray();
 +    $providerDefault = data_get($appSetting, 'PAYOUT_PROVIDER_DEFAULT', UserTransaction::PROVIDER_TRANSFER_VPBANK);
 +    
 +    if (!empty($userInfo->payout_provider_code)) {
 +        $accountName = !empty($options['account_name']) ? $options['account_name'] : '';
 +
 +        if ($userInfo->payout_provider_code == UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK && !Bank::isValidAccountNameByFirstName($accountName)) {
 +            return $providerDefault;
 +        }
 +        if ($userInfo->payout_provider_code == UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK && $bankInfo->code == Bank::BANK_CODE_TECHCOMBANK  && !Bank::isAccountNoTCB($accountNo)) {
 +            return $providerDefault;
 +        }
 +        if ($userInfo->payout_provider_code == UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK && !empty($accountNo) && (Bank::checkTypeCard($accountNo) || Bank::hasSpecialCharacter($accountNo) || Phone::isValid($accountNo))){
 +            return $providerDefault;
 +        }
 +
 +        if ($userInfo->payout_provider_code == UserTransaction::PROVIDER_TRANSFER_TPBANK && !empty($accountNo) && (Bank::checkTypeCard($accountNo) || Bank::hasSpecialCharacter($accountNo) || !is_numeric($accountNo) || Phone::isValid($accountNo))) {
 +            return $providerDefault;
 +        }
 +
 +        $payoutProviderSetting = !empty($userInfo->payout_provider_settings) ? explode(',', $userInfo->payout_provider_settings) : [];
 +        if ($payoutProviderSetting && is_array($payoutProviderSetting) && in_array($bankInfo->code, $payoutProviderSetting)) {
 +            return $bankInfo->code;
 +        }
 +
 +        return strtoupper($userInfo->payout_provider_code);
 +    }
 +
 +    if (!empty($options['amount']) && $options['amount'] >= config('setting.min_citad_amount') && in_array($userInfo->name, config('setting.merchant_citad'))) {
 +        return UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK;
 +    }
 +
 +    if(!is_numeric($accountNo) && !in_array($bankInfo->code, config('bank.bank_is_support_inick'))){
 +        return $providerDefault;
 +    }
 +    if (!empty($accountNo) && Bank::checkTypeCard($accountNo) && !in_array($bankInfo->code, config('bank.bank_is_support_card'))){
 +        $bankTransferAtm=data_get($appSetting, 'PAYOUT_PROVIDER_ATM', $providerDefault);
 +        if($bankTransferAtm==UserTransaction::PROVIDER_TRANSFER_BIDV && $bankInfo->code==Bank::BANK_CODE_MSB)
 +        {
 +            return $providerDefault;
 +        }
 +        return $bankTransferAtm;
 +    }
 +    if (!empty($accountNo) && Phone::isValid($accountNo) && !in_array($bankInfo->code, config('bank.bank_is_support_phone_number'))){
 +        return $providerDefault;
 +    }
 +    if (!empty($accountNo) && Bank::hasSpecialCharacter($accountNo)) {
 +        return $providerDefault;
 +    }
 +
 +    $transferBankSetting = TransferBankPartner::query()
 +        ->where('bank_code', $bankInfo->code)
 +        ->first();
 +    if (!$transferBankSetting instanceof TransferBankPartner) {
 +        return $providerDefault;
 +    }
 +    
 +    if(in_array($transferBankSetting->provider,[UserTransaction::PROVIDER_TRANSFER_TPBANK,UserTransaction::PROVIDER_TRANSFER_BIDV]) && in_array(Carbon::now()->hour,[23,0])){
 +        return $providerDefault;
 +    }
 +    if ($transferBankSetting->provider == UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK
 +        && $bankInfo->code == Bank::BANK_CODE_TECHCOMBANK
 +        && !Bank::isAccountNoTCB($accountNo)) {
 +        return $providerDefault;
 +    }
 +
 +    if ($transferBankSetting->provider == UserTransaction::PROVIDER_TRANSFER_TECHCOMBANK && !empty($accountNo) && (Bank::checkTypeCard($accountNo) || !is_numeric($accountNo) || Phone::isValid($accountNo))) {
 +        return $providerDefault;
 +    }
 +
 +    if ($transferBankSetting->provider == UserTransaction::PROVIDER_TRANSFER_TPBANK && !empty($accountNo) && (Bank::checkTypeCard($accountNo) || !is_numeric($accountNo) || Phone::isValid($accountNo))) {
 +        return $providerDefault;
 +    }
 +
 +    if ($transferBankSetting->provider == UserTransaction::PROVIDER_TRANSFER_MBBANK && !empty($options['amount']) && $options['amount'] > 4000000000) {
 +        return $providerDefault;
 +    }
 +
 +    return $transferBankSetting->provider;
 +}
 +</file>    
 +
 +**Ví dụ 1:**
 +
 +{{ :development:software-architecture:design-patterns:strategy-structure.png |}}
 +
 +<file php>
 +<?php
 +
 +namespace RefactoringGuru\Strategy\Conceptual;
 +
 +/**
 + * The Context defines the interface of interest to clients.
 + */
 +class Context
 +{
 +    /**
 +     * @var Strategy The Context maintains a reference to one of the Strategy
 +     * objects. The Context does not know the concrete class of a strategy. It
 +     * should work with all strategies via the Strategy interface.
 +     */
 +    private $strategy;
 +
 +    /**
 +     * Usually, the Context accepts a strategy through the constructor, but also
 +     * provides a setter to change it at runtime.
 +     */
 +    public function __construct(Strategy $strategy)
 +    {
 +        $this->strategy = $strategy;
 +    }
 +
 +    /**
 +     * Usually, the Context allows replacing a Strategy object at runtime.
 +     */
 +    public function setStrategy(Strategy $strategy)
 +    {
 +        $this->strategy = $strategy;
 +    }
 +
 +    /**
 +     * The Context delegates some work to the Strategy object instead of
 +     * implementing multiple versions of the algorithm on its own.
 +     */
 +    public function doSomeBusinessLogic(): void
 +    {
 +        // ...
 +
 +        echo "Context: Sorting data using the strategy (not sure how it'll do it)\n";
 +        $result = $this->strategy->doAlgorithm(["a", "b", "c", "d", "e"]);
 +        echo implode(",", $result) . "\n";
 +
 +        // ...
 +    }
 +}
 +
 +/**
 + * The Strategy interface declares operations common to all supported versions
 + * of some algorithm.
 + *
 + * The Context uses this interface to call the algorithm defined by Concrete
 + * Strategies.
 + */
 +interface Strategy
 +{
 +    public function doAlgorithm(array $data): array;
 +}
 +
 +/**
 + * Concrete Strategies implement the algorithm while following the base Strategy
 + * interface. The interface makes them interchangeable in the Context.
 + */
 +class ConcreteStrategyA implements Strategy
 +{
 +    public function doAlgorithm(array $data): array
 +    {
 +        sort($data);
 +
 +        return $data;
 +    }
 +}
 +
 +class ConcreteStrategyB implements Strategy
 +{
 +    public function doAlgorithm(array $data): array
 +    {
 +        rsort($data);
 +
 +        return $data;
 +    }
 +}
 +
 +/**
 + * The client code picks a concrete strategy and passes it to the context. The
 + * client should be aware of the differences between strategies in order to make
 + * the right choice.
 + */
 +$context = new Context(new ConcreteStrategyA());
 +echo "Client: Strategy is set to normal sorting.\n";
 +$context->doSomeBusinessLogic();
 +
 +echo "\n";
 +
 +echo "Client: Strategy is set to reverse sorting.\n";
 +$context->setStrategy(new ConcreteStrategyB());
 +$context->doSomeBusinessLogic();
 +</file>
 +
 +**Ví dụ 2:**
  
 <file php PaymentMethodInterface.php> <file php PaymentMethodInterface.php>
Line 196: Line 413:
 </file> </file>
  
-====== Tham khảo ======+===== Tham khảo =====
  
   * https://refactoring.guru/design-patterns/strategy   * https://refactoring.guru/design-patterns/strategy
 +  * https://www.youtube.com/watch?v=sheV2IBlQkU
   * https://www.youtube.com/watch?v=0VlakXgCukM   * https://www.youtube.com/watch?v=0VlakXgCukM
   * https://www.youtube.com/watch?v=sheV2IBlQkU   * https://www.youtube.com/watch?v=sheV2IBlQkU
  
  
development/software-architecture/design-patterns/strategy.1723481451.txt.gz · Last modified: 2024/08/12 16:50 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki