本文目录导读:

是的,PHP中的策略模式(Strategy Pattern) 非常适合封装算法,它的核心思想是:将一组可相互替换的算法封装成独立的策略类,使它们可以独立于客户端而变化。
策略模式的基本结构
<?php
// 1. 策略接口
interface SortStrategy {
public function sort(array $data): array;
}
// 2. 具体策略类 - 冒泡排序
class BubbleSortStrategy implements SortStrategy {
public function sort(array $data): array {
echo "使用冒泡排序\n";
$n = count($data);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($data[$j] > $data[$j + 1]) {
$temp = $data[$j];
$data[$j] = $data[$j + 1];
$data[$j + 1] = $temp;
}
}
}
return $data;
}
}
// 3. 具体策略类 - 快速排序
class QuickSortStrategy implements SortStrategy {
public function sort(array $data): array {
echo "使用快速排序\n";
if (count($data) < 2) {
return $data;
}
$pivot = $data[0];
$left = $right = [];
for ($i = 1; $i < count($data); $i++) {
if ($data[$i] < $pivot) {
$left[] = $data[$i];
} else {
$right[] = $data[$i];
}
}
return array_merge(
$this->sort($left),
[$pivot],
$this->sort($right)
);
}
}
// 4. 上下文类(Context)
class Sorter {
private $strategy;
public function __construct(SortStrategy $strategy) {
$this->strategy = $strategy;
}
public function setStrategy(SortStrategy $strategy) {
$this->strategy = $strategy;
}
public function sortData(array $data): array {
return $this->strategy->sort($data);
}
}
// 5. 客户端使用
$data = [3, 1, 4, 1, 5, 9, 2, 6];
$sorter = new Sorter(new BubbleSortStrategy());
$result = $sorter->sortData($data);
print_r($result);
// 运行时切换算法
$sorter->setStrategy(new QuickSortStrategy());
$result = $sorter->sortData($data);
print_r($result);
更实际的应用示例:支付系统
<?php
// 支付策略接口
interface PaymentStrategy {
public function pay(float $amount): string;
}
// 支付宝支付
class AlipayPayment implements PaymentStrategy {
private $accountNumber;
public function __construct(string $accountNumber) {
$this->accountNumber = $accountNumber;
}
public function pay(float $amount): string {
return "支付宝支付 {$amount} 元,账号: {$this->accountNumber}";
}
}
// 微信支付
class WechatPayment implements PaymentStrategy {
private $openId;
public function __construct(string $openId) {
$this->openId = $openId;
}
public function pay(float $amount): string {
return "微信支付 {$amount} 元,OpenID: {$this->openId}";
}
}
// 银行转账
class BankTransfer implements PaymentStrategy {
private $bankAccount;
public function __construct(string $bankAccount) {
$this->bankAccount = $bankAccount;
}
public function pay(float $amount): string {
return "银行转账 {$amount} 元,账户: {$this->bankAccount}";
}
}
// 订单处理上下文
class Order {
private $paymentStrategy;
private $total = 0;
public function setPaymentStrategy(PaymentStrategy $strategy) {
$this->paymentStrategy = $strategy;
}
public function setTotal(float $total) {
$this->total = $total;
}
public function checkout(): string {
if (!$this->paymentStrategy) {
throw new Exception("请先选择支付方式");
}
return $this->paymentStrategy->pay($this->total);
}
}
// 客户端使用
$order = new Order();
$order->setTotal(100.00);
// 用户选择支付宝支付
$order->setPaymentStrategy(new AlipayPayment('138****8888'));
echo $order->checkout() . "\n";
// 切换到微信支付
$order->setPaymentStrategy(new WechatPayment('wx_abc123'));
echo $order->checkout() . "\n";
策略模式的优点
- 开闭原则:新增算法不需要修改现有代码
- 减少条件语句:避免大量的
if-else或switch-case - 提高复用性:算法可以独立于上下文被复用
- 运行时切换:可以在运行时动态选择算法
适用场景
- 需要根据不同条件使用不同算法
- 算法需要经常被替换或扩展
- 算法之间存在类似行为但实现不同
- 需要避免大量条件判断语句
策略模式是PHP开发中非常实用的设计模式,特别适合需要封装和切换算法的场景。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播