development:software-architecture:design-patterns:strategy
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:software-architecture:design-patterns:strategy [2024/08/13 04:20] – tungnt | development:software-architecture:design-patterns:strategy [2024/08/19 09:41] (current) – tungnt | ||
---|---|---|---|
Line 6: | Line 6: | ||
<file php> | <file php> | ||
- | private static | + | function getProviderTransfer($userInfo, |
- | { | + | { |
- | | + | if(!empty($accountNo) && preg_match('/ |
- | return UserTransaction:: | + | return UserTransaction:: |
- | } | + | } |
- | | + | if (!empty($accountNo) && preg_match('/ |
- | return UserTransaction:: | + | return UserTransaction:: |
- | } | + | } |
- | if (!empty($accountNo) && preg_match('/ | + | if (!empty($options[' |
- | return UserTransaction:: | + | if ($bankInfo-> |
- | } | + | |
- | if (!empty($options[' | + | |
- | if ($bankInfo-> | + | |
- | return UserTransaction:: | + | |
- | } | + | |
- | + | ||
- | if (in_array($bankInfo-> | + | |
- | UserTransaction:: | + | |
- | UserTransaction:: | + | |
- | UserTransaction:: | + | |
- | return $bankInfo-> | + | |
- | } | + | |
return UserTransaction:: | return UserTransaction:: | ||
} | } | ||
- | | + | if (in_array($bankInfo-> |
- | | + | UserTransaction:: |
- | $payoutMsbCardType = Cache:: | + | UserTransaction:: |
- | if($payoutMsbCardType == ' | + | UserTransaction:: |
- | return UserTransaction:: | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | if($userInfo-> | + | |
- | if(!empty($options[' | + | |
- | //return UserTransaction:: | + | |
- | | + | |
- | } | + | |
- | + | ||
- | $appSetting = AppSetting:: | + | |
- | $providerDefault = data_get($appSetting, | + | |
- | $providerMerchantXDefault = data_get($appSetting, ' | + | |
- | if (!empty($userInfo-> | + | |
- | $accountName = !empty($options[' | + | |
- | + | ||
- | if ($userInfo-> | + | |
- | return strtoupper($userInfo-> | + | |
- | } | + | |
- | + | ||
- | if ($userInfo-> | + | |
- | return $providerDefault; | + | |
- | | + | |
- | if ($userInfo-> | + | |
- | return $providerDefault; | + | |
- | } | + | |
- | if ($userInfo-> | + | |
- | return $providerDefault; | + | |
- | | + | |
- | + | ||
- | if ($userInfo-> | + | |
- | return $providerDefault; | + | |
- | } | + | |
- | + | ||
- | $payoutProviderSetting = !empty($userInfo-> | + | |
- | if ($payoutProviderSetting && is_array($payoutProviderSetting) && in_array($bankInfo-> | + | |
return $bankInfo-> | return $bankInfo-> | ||
- | } | ||
- | |||
- | return strtoupper($userInfo-> | ||
} | } | ||
+ | return UserTransaction:: | ||
+ | } | ||
- | if($userInfo->is_merchant_x==1 && | + | |
- | | + | $providerDefault |
- | } | + | |
+ | if (!empty($userInfo-> | ||
+ | | ||
- | if($userInfo-> | + | if ($userInfo-> |
- | return UserTransaction:: | + | |
- | } | + | |
- | + | ||
- | if (!empty($options[' | + | |
- | return UserTransaction:: | + | |
- | } | + | |
- | + | ||
- | if(!is_numeric($accountNo) && !in_array($bankInfo-> | + | |
return $providerDefault; | return $providerDefault; | ||
} | } | ||
- | if (!empty($accountNo) && Bank:: | + | if ($userInfo->payout_provider_code |
- | $bankTransferAtm=data_get($appSetting, | + | |
- | if($bankTransferAtm==UserTransaction:: | + | |
- | { | + | |
- | return $providerDefault; | + | |
- | } | + | |
- | return $bankTransferAtm; | + | |
- | } | + | |
- | if (!empty($accountNo) | + | |
return $providerDefault; | return $providerDefault; | ||
} | } | ||
- | if (!empty($accountNo) && Bank:: | + | if ($userInfo-> |
return $providerDefault; | return $providerDefault; | ||
} | } | ||
- | | + | if ($userInfo->payout_provider_code == UserTransaction:: |
- | -> | + | |
- | -> | + | |
- | | + | |
- | return $providerDefault; | + | |
- | } | + | |
- | + | ||
- | if(in_array($transferBankSetting->provider,[UserTransaction:: | + | |
- | return $providerDefault; | + | |
- | } | + | |
- | if ($transferBankSetting-> | + | |
- | && | + | |
- | && | + | |
return $providerDefault; | return $providerDefault; | ||
} | } | ||
- | | + | $payoutProviderSetting |
- | return $providerDefault; | + | if ($payoutProviderSetting && is_array($payoutProviderSetting) && in_array($bankInfo-> |
+ | return $bankInfo-> | ||
} | } | ||
- | | + | |
- | return $providerDefault; | + | } |
- | } | + | |
- | | + | |
+ | return UserTransaction:: | ||
+ | } | ||
+ | |||
+ | if(!is_numeric($accountNo) && !in_array($bankInfo-> | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | if (!empty($accountNo) && Bank:: | ||
+ | $bankTransferAtm=data_get($appSetting, | ||
+ | if($bankTransferAtm==UserTransaction:: | ||
+ | | ||
return $providerDefault; | return $providerDefault; | ||
} | } | ||
+ | return $bankTransferAtm; | ||
+ | } | ||
+ | if (!empty($accountNo) && Phone:: | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | if (!empty($accountNo) && Bank:: | ||
+ | return $providerDefault; | ||
+ | } | ||
- | return | + | |
+ | | ||
+ | -> | ||
+ | if (!$transferBankSetting instanceof TransferBankPartner) { | ||
+ | return $providerDefault; | ||
} | } | ||
+ | | ||
+ | if(in_array($transferBankSetting-> | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | if ($transferBankSetting-> | ||
+ | && $bankInfo-> | ||
+ | && !Bank:: | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | |||
+ | if ($transferBankSetting-> | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | |||
+ | if ($transferBankSetting-> | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | |||
+ | if ($transferBankSetting-> | ||
+ | return $providerDefault; | ||
+ | } | ||
+ | |||
+ | return $transferBankSetting-> | ||
+ | } | ||
</ | </ | ||
- | **Ví dụ về Strategy | + | **Ví dụ 1:** |
+ | |||
+ | {{ : | ||
+ | |||
+ | <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, | ||
+ | * provides a setter to change it at runtime. | ||
+ | */ | ||
+ | public function __construct(Strategy $strategy) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Usually, the Context allows replacing a Strategy object at runtime. | ||
+ | */ | ||
+ | public function setStrategy(Strategy $strategy) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The Context delegates some work to the Strategy object instead of | ||
+ | * implementing multiple versions of the algorithm on its own. | ||
+ | */ | ||
+ | public function doSomeBusinessLogic(): | ||
+ | { | ||
+ | // ... | ||
+ | |||
+ | echo " | ||
+ | $result = $this-> | ||
+ | echo implode(",", | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 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 " | ||
+ | $context-> | ||
+ | |||
+ | echo " | ||
+ | |||
+ | echo " | ||
+ | $context-> | ||
+ | $context-> | ||
+ | </ | ||
+ | |||
+ | **Ví dụ 2:** | ||
<file php PaymentMethodInterface.php> | <file php PaymentMethodInterface.php> | ||
Line 336: | Line 413: | ||
</ | </ | ||
- | ====== Tham khảo | + | ===== Tham khảo ===== |
* https:// | * https:// | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
+ | * https:// | ||
development/software-architecture/design-patterns/strategy.1723522823.txt.gz · Last modified: 2024/08/13 04:20 by tungnt