策略模式:动态切换算法或行为,适用于不同策略处理相同任务。装饰器模式:在不修改原始类基础上向对象动态添加功能,适用于向对象动态添加功能或行为。外观模式:为复杂子系统或接口提供简化和统一的接口,适用于复杂子系统或接口提供统一接口。

如何选择最合适的 PHP 函数设计模式?
设计模式是一组重复出现的代码结构,旨在解决特定类型的编程问题。了解不同的设计模式对于提高代码的可重用性、模块性和易维护性至关重要。在 PHP 中,有几个函数设计模式可用于各种场景。
策略模式
立即学习“PHP免费学习笔记(深入)”;
策略模式允许您在运行时动态切换算法或行为。它特别适用于需要使用不同策略处理相同任务的情况。
现代化家居响应式网站模板源码是以cmseasy进行开发的家居网站模板。该软件可免费使用,模板附带测试数据!模板源码特点:整体采用浅色宽屏设计,简洁大气,电脑手机自适应布局,大方美观,功能齐全,值得推荐的一款模板,每个页面精心设计,美观大方,兼容各大浏览器;所有代码经过SEO优化,使网站更利于搜索引擎排名,是您做环保类网站的明确选择。无论是在电脑、平板、手机上都可以访问到排版合适的网站,即便是微信等
0
interface Strategy {
public function doSomething(): void;
}
class ConcreteStrategyA implements Strategy {
public function doSomething(): void {
echo "Doing something in strategy A";
}
}
class ConcreteStrategyB implements Strategy {
public function doSomething(): void {
echo "Doing something in strategy B";
}
}
class Context {
private $strategy;
public function __construct(Strategy $strategy) {
$this->strategy = $strategy;
}
public function doSomething(): void {
$this->strategy->doSomething();
}
}
// 实战案例
$contextA = new Context(new ConcreteStrategyA());
$contextA->doSomething(); // Doing something in strategy A
$contextB = new Context(new ConcreteStrategyB());
$contextB->doSomething(); // Doing something in strategy B装饰器模式
装饰器模式允许您在不修改原始类的基础上向对象动态添加功能或行为。
interface Beverage {
public function getDescription(): string;
public function cost(): float;
}
class Espresso implements Beverage {
public function getDescription(): string {
return "Espresso";
}
public function cost(): float {
return 1.99;
}
}
class Mocha implements Beverage {
private $beverage;
public function __construct(Beverage $beverage) {
$this->beverage = $beverage;
}
public function getDescription(): string {
return $this->beverage->getDescription() . ", Mocha";
}
public function cost(): float {
return $this->beverage->cost() + 0.20;
}
}
class Whip implements Beverage {
private $beverage;
public function __construct(Beverage $beverage) {
$this->beverage = $beverage;
}
public function getDescription(): string {
return $this->beverage->getDescription() . ", Whip";
}
public function cost(): float {
return $this->beverage->cost() + 0.10;
}
}
// 实战案例
$beverage = new Espresso();
echo $beverage->getDescription() . " $" . $beverage->cost() . "\n"; // Espresso $1.99
$beverage2 = new Mocha($beverage);
echo $beverage2->getDescription() . " $" . $beverage2->cost() . "\n"; // Espresso, Mocha $2.19
$beverage3 = new Whip($beverage2);
echo $beverage3->getDescription() . " $" . $beverage3->cost() . "\n"; // Espresso, Mocha, Whip $2.29外观模式
外观模式为复杂的子系统或一组接口提供了一个简化和统一的接口。
interface CreditCard {
public function getCreditCardNumber(): string;
public function getCVV(): string;
public function getExpirationDate(): string;
}
class VisaCreditCard implements CreditCard {
private $number;
private $cvv;
private $expirationDate;
public function __construct($number, $cvv, $expirationDate) {
$this->number = $number;
$this->cvv = $cvv;
$this->expirationDate = $expirationDate;
}
public function getCreditCardNumber(): string {
return $this->number;
}
public function getCVV(): string {
return $this->cvv;
}
public function getExpirationDate(): string {
return $this->expirationDate;
}
}
class MasterCardCreditCard implements CreditCard {
private $number;
private $cvv;
private $expirationDate;
public function __construct($number, $cvv, $expirationDate) {
$this->number = $number;
$this->cvv = $cvv;
$this->expirationDate = $expirationDate;
}
public function getCreditCardNumber(): string {
return $this->number;
}
public function getCVV(): string {
return $this->cvv;
}
public function getExpirationDate(): string {
return $this->expirationDate;
}
}
class CreditCardFacade {
private $creditCard;
public function __construct(CreditCard $creditCard) {
$this->creditCard = $creditCard;
}
public function makePayment($amount) {
// Perform payment logic using the credit card information
echo "Payment made using " . $this->creditCard->getCreditCardNumber();
}
}
// 实战案例
$visaCard = new VisaCreditCard("4111111111111111", "123", "12/24");
$masterCard = new MasterCardCreditCard("5555555555555555", "456", "11/25");
$facade1 = new CreditCardFacade($visaCard);
$facade1->makePayment(100); // Payment made using 4111111111111111
$facade2 = new CreditCardFacade($masterCard);
$facade2->makePayment(200); // Payment made using 5555555555555555了解这些设计模式的优点和限制将帮助您在 PHP 中做出最合适的决策。通过遵循设计原则和最佳实践,您可以创建可扩展且易于维护的代码。
以上就是如何选择最合适的 PHP 函数设计模式?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号