PHP设计模式是软件开发中常用的解决方案,能够帮助你更高效地构建和维护代码。以下是一些常见的设计模式及其示例的简要描述:
1. 单例模式(Singleton)
确保一个类只有一个实例,并提供全局访问点。
class Singleton {
private static $instance = null;
private function __construct() {
// 私有构造函数防止外部实例化
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Singleton();
}
return self::$instance;
}
}2. 工厂模式(Factory)
提供一个创建对象的接口,但由子类决定要实例化的类是哪一个。
interface Vehicle {
public function drive();
}
class Car implements Vehicle {
public function drive() {
return "Driving a car";
}
}
class Truck implements Vehicle {
public function drive() {
return "Driving a truck";
}
}
class VehicleFactory {
public static function createVehicle($type) {
switch ($type) {
case 'car':
return new Car();
case 'truck':
return new Truck();
default:
throw new Exception("Unknown vehicle type");
}
}
}
// 使用工厂模式创建对象
$car = VehicleFactory::createVehicle('car');
echo $car->drive();3. 策略模式(Strategy)
定义一系列算法,将每一个算法封装起来,并使它们可以互换。
interface Strategy {
public function doOperation($num1, $num2);
}
class OperationAdd implements Strategy {
public function doOperation($num1, $num2) {
return $num1 + $num2;
}
}
class OperationSubtract implements Strategy {
public function doOperation($num1, $num2) {
return $num1 - $num2;
}
}
class OperationMultiply implements Strategy {
public function doOperation($num1, $num2) {
return $num1 * $num2;
}
}
class Context {
private $strategy;
public function __construct(Strategy $strategy) {
$this->strategy = $strategy;
}
public function executeStrategy($num1, $num2) {
return $this->strategy->doOperation($num1, $num2);
}
}
// 使用策略模式
$context = new Context(new OperationAdd());
echo $context->executeStrategy(10, 5); // 输出 15
$context = new Context(new OperationSubtract());
echo $context->executeStrategy(10, 5); // 输出 5
$context = new Context(new OperationMultiply());
echo $context->executeStrategy(10, 5); // 输出 504. 观察者模式(Observer)
定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象的状态变化。
interface Observer {
public function update($eventData);
}
interface Observable {
public function attach(Observer $observer);
public function detach(Observer $observer);
public function notify($eventData);
}
class Event implements Observable {
private $observers = [];
public function attach(Observer $observer) {
$this->observers[] = $observer;
}
public function detach(Observer $observer) {
$index = array_search($observer, $this->observers, true);
if ($index !== false) {
unset($this->observers[$index]);
}
}
public function notify($eventData) {
foreach ($this->observers as $observer) {
$observer->update($eventData);
}
}
}
class EmailNotification implements Observer {
public function update($eventData) {
echo "Email sent with data: $eventData\n";
}
}
class SMSNotification implements Observer {
public function update($eventData) {
echo "SMS sent with data: $eventData\n";
}
}
// 使用观察者模式
$event = new Event();
$event->attach(new EmailNotification());
$event->attach(new SMSNotification());
$event->notify("User registered");5. 装饰者模式(Decorator)
动态地给一个对象添加一些额外的职责。
interface Component {
public function operation();
}
class ConcreteComponent implements Component {
public function operation() {
return "ConcreteComponent";
}
}
class Decorator implements Component {
protected $component;
public function __construct(Component $component) {
$this->component = $component;
}
public function operation() {
return $this->component->operation();
}
}
class ConcreteDecoratorA extends Decorator {
public function operation() {
return "ConcreteDecoratorA(" . parent::operation() . ")";
}
}这些示例只是PHP中众多设计模式中的一部分,具体实现可能会根据需求有所不同。你可以根据这些模式的结构图,结合实际情况进行扩展和修改。
THE END
匿名
2025-11-09
https://collaigo.com 免费在线拼图工具
匿名
2025-10-22
盖楼盖楼!
匿名
2025-08-11
沙发沙发
匿名
2025-08-10
https://at.oiik.cn/bing.html
匿名
2025-02-21
实用,我在开发https://minmail.app/时候使用到了
王飞翔
2024-12-30
亲爱的朋友:您好!中国疫情持续蔓延,很多人症状非常严重持久不愈,医院人满为患,各年龄段随地倒猝死的现象暴增,多省感染手足口、甲流、乙流、支原体、合胞及腺病毒的儿童不断攀升,目前各种天灾人祸,天气异象频发。古今中外的很多预言都说了这几年人类有大灾难,如刘伯温在预言中说 “贫者一万留一千,富者一万留二三”,“贫富若不回心转,看看死期到眼前”, 预言中也告诉世人如何逃离劫难的方法,真心希望您能躲过末劫中的劫难,有个美好的未来,请您务必打开下方网址认真了解,内有躲避瘟疫保平安的方法。网址1:https://github.com/1992513/www/blob/master/README.md?abhgc#1 网址2:bitly.net/55bbbb 网址3:https://d3ankibxiji86m.cloudfront.net/30gj 如打不开请多换几个浏览器试