有点通了
<?php /* The bridge pattern is used when we want to decouple a class or abstraction from its implementation, allowing them both to change independently. This is useful when the class and its implementation vary often */ interface MessagingInterface { public function send($body); } class TextMessage implements MessagingInterface { public function send($body) { echo "TextMessage > send > $body: <br/>"; } } class HtmlMessage implements MessagingInterface { public function send($body) { echo "HtmlMessage > send > $body: <br/>"; } } interface MailerInterface { public function setSender(MessagingInterface $sender); public function send($body); } abstract class Mailer implements MailerInterface { protected $sender; public function setSender(MessagingInterface $sender) { $this->sender = $sender; } } class PHPMailer extends Mailer { public function send($body) { $body .= "\n\n Set from a phpmailer ."; return $this->sender->send($body); } } class SwiftMailer extends Mailer { public function send($body) { $body .= "\n\n Set from a swiftmailer ."; return $this->sender->send($body); } } $phpMailer = new PHPMailer(); $phpMailer->setSender(new TextMessage()); $phpMailer->send(‘Hi!‘); $swiftMailer = new SwiftMailer(); $swiftMailer->setSender(new HtmlMessage()); $swiftMailer->send(‘Hello!‘); ?>
原文地址:https://www.cnblogs.com/aguncn/p/11181779.html
时间: 2024-10-07 19:55:50