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); // 输出 50
4. 观察者模式(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中众多设计模式中的一部分,具体实现可能会根据需求有所不同。你可以根据这些模式的结构图,结合实际情况进行扩展和修改。
发表评论 取消回复