TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:software-architecture:design-patterns:strategy

This is an old revision of the document!


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ì.

PaymentMethodInterface.php
<?php
 
namespace App\Services\Payment;
 
interface PaymentMethodInterface
{                                
    /**
     * pay
     *
     * @return string
     */
    public function pay(): string;     
 
}
CreditCardService.php
<?php
 
namespace App\Services\Payment;
 
/**
 * CreditCardService
 */
class CreditCardService implements PaymentMethodInterface
{                    
 
    /**
     * pay
     *
     * @return string
     */
    public function pay(): string{
        return 'CreditCard';
    }
 
}
ApplePayService.php
<?php
 
namespace App\Services\Payment;
 
/**
 * ApplePayService
 */
class ApplePayService implements PaymentMethodInterface
{                    
 
    /**
     * pay
     *
     * @return string
     */
    public function pay(): string{
        return 'ApplePay';
    }
 
}
GooglePayService.php
<?php
 
namespace App\Services\Payment;
 
/**
 * GooglePayService
 */
class GooglePayService implements PaymentMethodInterface
{                    
 
    /**
     * pay
     *
     * @return string
     */
    public function pay(): string{
        return 'GooglePay';
    }
 
}
PaymentContextService.php
<?php
 
namespace App\Services\Payment;
 
/**
 * PaymentContextService
 */
class PaymentContextService
{                    
 
    /**
     * @var PaymentMethodInterface
     */
    private $paymentMethod;
 
    /**
     * __construct
     *
     * @param  mixed $paymentMethod
     * @return void
     */
    public function __construct(string $paymentMethod)
    {     
        $this->paymentMethod = match ($paymentMethod){
            'credit' => new CreditCardService(),
            'apple' => new ApplePayService(),
            'google' => new GooglePayService(),
            default => throw new \InvalidArgumentException("You must pass in either credit, apple or google as the payment method.")
        };
    }
 
    /**
     * pay
     *
     * @return string
     */
    public function pay(){
        return $this->paymentMethod->pay();
    }
 
}
StrategyCommand.php
<?php
 
namespace App\Console\Commands\Test;
 
use App\Services\Payment\PaymentContextService;
use Illuminate\Console\Command;
 
/**
 * Class StrategyCommand
 * @package Modules\Web\Console\Commands
 */
class StrategyCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'test:strategy {paymentMethod}';
 
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Test Cms module command";
 
    public function handle(){
        //$result = $this->normal($this->argument('paymentMethod'));
        $result = $this->strategy($this->argument('paymentMethod'));
 
        $this->line($result);
    }
 
    private function strategy($paymentMethod){
        $payment = new PaymentContextService($paymentMethod);
 
        return $payment->pay();
    }
 
    /**
     * normal
     *
     * @param  mixed $paymentMethod
     * @return string
     */
    private function normal($paymentMethod){
        if('credit' == $paymentMethod){
            return 'CreditCard';
        }
 
        if('google' == $paymentMethod){
            return 'GooglePay';
        }
 
        if('apple' == $paymentMethod){
            return 'ApplePay';
        }
 
        throw new \InvalidArgumentException("You must pass in either credit, apple or google as the payment method.");
    }
}

Tham khảo

development/software-architecture/design-patterns/strategy.1723481964.txt.gz · Last modified: 2024/08/12 16:59 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki