zoukankan      html  css  js  c++  java
  • 一道面试题,观察者模式

    <?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
  • 相关阅读:
    【leetcode】面试题 01.04. 回文排列
    【leetcode】将数组分成和相等的三个部分
    【leetcode】杨辉三角
    【leetcode】杨辉三角 II
    【leetcode】判断路径是否相交
    【leetcode】路径总和
    【leetcode】山脉数组的峰顶索引
    053-4
    053-151
    053-272
  • 原文地址:https://www.cnblogs.com/polax/p/11770351.html
Copyright © 2011-2022 走看看