【问题标题】:Which design pattern should I use for notifications?我应该为通知使用哪种设计模式?
【发布时间】:2019-09-10 09:28:16
【问题描述】:

我正在使用队列开发 laravel 项目。 我的“checkAgeAndNotify”API 将检查年龄是否大于 50,然后它会使用队列向该用户发送电子邮件通知。

class Age extends Controller
 {
    public function checkAgeAndNotify(Request $request)
    {
       if(Input::get('age') > 50){
           $job = (new notification());
           dispatch($job);
       }
    }
 }

以下是我的工作课程:

class notification implements ShouldQueue
 {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public construct(){}

    public function handle()
    {
        Mail::send('welcome', $data, function ($message) use ($data) {
                $message->from('my-test@gmail.com', 'unknown-sender');
                $message->to('alpha@test.com')->subject('Test Email');
        });
    }

}

我的代码 100% 完美运行。但是我不确定如何根据情况使用任何设计模式来转换我的代码。

如果不能在这个小代码中应用模式。因此,我们可以假设我们有多个渠道来发送通知,例如电子邮件、短信等。

【问题讨论】:

  • 如果“代码 100% 完美运行”那么为什么要添加任何设计模式?
  • 嗯,实现设计模式是我教授的要求。
  • 您可以使用命令事件模式或观察者模式。
  • 考虑重命名你的类并使用一致的大小写,例如AgeNotifier 和 NotificationJob

标签: php laravel-5 design-patterns notifications queue


【解决方案1】:

您的教授一定会希望您实现观察者模式,因为观察者(用户)会收到通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多