码迷,mamicode.com
首页 > 其他好文 > 详细

一道面试题,观察者模式

时间:2019-10-31 11:55:06      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:短信   ret   ges   style   说明   nts   UNC   fun   interface   

<?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

 

一道面试题,观察者模式

标签:短信   ret   ges   style   说明   nts   UNC   fun   interface   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!