标签:发送邮件 操作 blog put 表示 user 积分 file 访问
表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。
场景:我们需要在用户积分增加的时候,写入一个日志文件,同时给该用户发送一个邮件. interface ActionHandle { public function handle(User $user); } class User { public $point; public $uid; public $name; public function __construct($point, $uid, $name) { $this->point = $point; $this->uid = $uid; $this->name = $name; } public function addPoint($dValue) { $this->point += $dValue; } public function handle(ActionHandle $handle) { $handle->handle($this); } } class Log implements ActionHandle { public function handle(User $user) { file_put_contents(‘log.txt‘, "{$user->uid} {$user->name} 操作积分: 操作后积分{$user->point}"); } } class Email implements ActionHandle { public function handle(User $user) { $title = "您的积分增加了"; $content = "{$user->uid} {$user->name} 操作积分: 操作后积分{$user->point}"; $this->sendEmail($title, $content); } protected function sendEmail($title, $content) { // 发送邮件 echo $title; echo $content; } } $user = new User(500, 1, ‘jack‘); $user->addPoint(100); $log = new Log(); $user->handle($log); $email = new Email(); $user->handle($email);
标签:发送邮件 操作 blog put 表示 user 积分 file 访问
原文地址:http://www.cnblogs.com/itfenqing/p/7750613.html