首页 > 其他 > 详细

一道面试题,观察者模式

时间:2019-10-31 13:25:12      阅读:142      评论:0      收藏:0      [点我收藏+]
<?php

interface Strategy
{
  public function send_code($code);
}

class messageStrategy implements Strategy
{
  public function send_code($code)
  {
    return "短信已发送";
  }
}

class emailStrategy implements Strategy
{
  public function send_code($code)
  {
    return "邮件已发送";
  }
}

class weixinStrategy implements Strategy
{
  public function send_code($code)
  {
    return "微信已发送";
  }
}


class sendCode
{
  private $strategyInstance;
  public function __construct ($instance)
  {
    $this->strategyInstance = $instance;
  }
 
  public function send($code)
  {
    return $this->strategyInstance->send_code($code);
  }
}


$type = $_GET(‘type‘);

if($type==‘weixin‘){
    $instance = new weixinStrategy();
}elseif($type==‘message‘){
    $instance = new messageStrategy();
}elseif ($type==‘email‘) {
    $instance = new emailStrategy();
}



$class = new sendCode($instance);
$code =  rand(10000,99999);
$class->send($code);


?>




1 使用场景说明
用户注册/登录/忘记密码三种情况下,需要发送验证码

2 接口说明
   后台系统实现推送验证码功能,支持短信/邮件/微信模版消息。

3  请求参数
type:发送的类型 weixin 或 message 或 email

 

一道面试题,观察者模式

原文:https://www.cnblogs.com/polax/p/11770351.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!