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/13 04:24] tungntdevelopment:software-architecture:design-patterns:strategy [2024/08/19 09:41] (current) tungnt
Line 30: Line 30:
     $appSetting = AppSetting::get()->pluck('content','key')->toArray();     $appSetting = AppSetting::get()->pluck('content','key')->toArray();
     $providerDefault = data_get($appSetting, 'PAYOUT_PROVIDER_DEFAULT', UserTransaction::PROVIDER_TRANSFER_VPBANK);     $providerDefault = data_get($appSetting, 'PAYOUT_PROVIDER_DEFAULT', UserTransaction::PROVIDER_TRANSFER_VPBANK);
-    $providerMerchantXDefault = data_get($appSetting, 'PAYOUT_PROVIDER_MERCHANT_IS_X', '');+    
     if (!empty($userInfo->payout_provider_code)) {     if (!empty($userInfo->payout_provider_code)) {
         $accountName = !empty($options['account_name']) ? $options['account_name'] : '';         $accountName = !empty($options['account_name']) ? $options['account_name'] : '';
Line 110: Line 110:
 </file>     </file>    
  
-**Ví dụ về Strategy Pattern:**+**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 307: 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=sheV2IBlQkU
   * https://www.youtube.com/watch?v=0VlakXgCukM   * https://www.youtube.com/watch?v=0VlakXgCukM
 +  * https://www.youtube.com/watch?v=sheV2IBlQkU
  
  
development/software-architecture/design-patterns/strategy.1723523062.txt.gz · Last modified: 2024/08/13 04:24 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki