
<?php
// 抽象产品接口
interface NotificationInterface {
/**
* 发送通知
* @param string $message 消息内容
* @param string $recipient 接收者
* @return bool 发送是否成功
*/
public function send(string $message, string $recipient): bool;
}
<?php
// 具体产品:邮件通知
class EmailNotification implements NotificationInterface {
private $smtpServer;
private $username;
private $password;
public function __construct(string $smtpServer, string $username, string $password) {
$this->smtpServer = $smtpServer;
$this->username = $username;
$this->password = $password;
}
public function send(string $message, string $recipient): bool {
// 实际项目中这里会有SMTP发送逻辑
echo "通过邮件发送给 {$recipient}:{$message}\n";
return true;
}
}
// 具体产品:短信通知
class SmsNotification implements NotificationInterface {
private $apiKey;
private $apiSecret;
public function __construct(string $apiKey, string $apiSecret) {
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
}
public function send(string $message, string $recipient): bool {
// 实际项目中这里会有短信API调用逻辑
echo "通过短信发送给 {$recipient}:{$message}\n";
return true;
}
}
// 具体产品:推送通知
class PushNotification implements NotificationInterface {
private $appId;
private $appSecret;
public function __construct(string $appId, string $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function send(string $message, string $recipient): bool {
// 实际项目中这里会有推送API调用逻辑
echo "通过推送发送给 {$recipient}:{$message}\n";
return true;
}
}
<?php
// 抽象工厂接口
interface NotificationFactoryInterface {
/**
* 创建通知对象
* @return NotificationInterface
*/
public function createNotification(): NotificationInterface;
}
<?php
// 具体工厂:邮件通知工厂
class EmailNotificationFactory implements NotificationFactoryInterface {
private $smtpServer;
private $username;
private $password;
public function __construct(string $smtpServer, string $username, string $password) {
$this->smtpServer = $smtpServer;
$this->username = $username;
$this->password = $password;
}
public function createNotification(): NotificationInterface {
return new EmailNotification(
$this->smtpServer,
$this->username,
$this->password
);
}
}
// 具体工厂:短信通知工厂
class SmsNotificationFactory implements NotificationFactoryInterface {
private $apiKey;
private $apiSecret;
public function __construct(string $apiKey, string $apiSecret) {
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
}
public function createNotification(): NotificationInterface {
return new SmsNotification($this->apiKey, $this->apiSecret);
}
}
// 具体工厂:推送通知工厂
class PushNotificationFactory implements NotificationFactoryInterface {
private $appId;
private $appSecret;
public function __construct(string $appId, string $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function createNotification(): NotificationInterface {
return new PushNotification($this->appId, $this->appSecret);
}
}
通过邮件发送给 newuser@example.com:欢迎注册我们的网站
通过短信发送给 13800138000:您的验证码是123456
通过推送发送给 user123:您有一条新消息
WechatNotification
实现 NotificationInterface
WechatNotificationFactory
实现 NotificationFactoryInterface