当前位置: 首页 > wzjs >正文

c 怎么和网站做交互西地那非片能延时多久有副作用吗

c 怎么和网站做交互,西地那非片能延时多久有副作用吗,深圳做网站jm3q,wordpress手机发布文章基于策略模式实现灵活可扩展的短信服务架构 引言 在企业级应用开发中,短信服务是不可或缺的基础功能之一。随着业务发展,我们可能需要接入多个短信服务提供商(如阿里云、腾讯云、第三方短信网关等),并能够在不修改核…

基于策略模式实现灵活可扩展的短信服务架构

引言

在企业级应用开发中,短信服务是不可或缺的基础功能之一。随着业务发展,我们可能需要接入多个短信服务提供商(如阿里云、腾讯云、第三方短信网关等),并能够在不修改核心业务代码的情况下灵活切换。本文将介绍如何使用策略模式设计一个高扩展性的短信服务架构,并结合实际代码示例进行讲解。

策略模式简介

策略模式(Strategy Pattern)是一种行为设计模式,它定义了一系列算法,并将每个算法封装起来,使它们可以互相替换。策略模式让算法的变化独立于使用算法的客户端。

策略模式的三大角色

  1. 策略接口(Strategy Interface):定义所有支持的算法的公共接口
  2. 具体策略(Concrete Strategies):实现策略接口的具体算法类
  3. 上下文(Context):持有一个策略对象的引用,并将客户端请求委托给策略对象

短信服务架构设计

1. 策略接口设计

@Service
public interface SmsComInterFace {public R sendSms(String phoneNumber, Map<String, Object> templateParams);
}

这里使用了Spring的@Service注解标记接口,虽然对接口使用@Service不是必须的,但在某些框架中可以辅助组件扫描。

2. 具体策略实现

以阿里云短信服务为例,实现具体的策略:

package com.ruoyi.sms.config;import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.sms.inter.SmsComInterFace;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.util.Map;@Component
public class AliyunSmsUtil implements SmsComInterFace {@Value("${aliyun.sms.sms-access-key-id}")private String accessKeyId;@Value("${aliyun.sms.sms-access-key-secret}")private String accessKeySecret;@Value("${aliyun.sms.sms-sign-nam}")private String signName;@Value("${aliyun.sms.sms-template-code}")private String templateCode;@Value("${aliyun.sms.sms-endpoint}")private String endpoint;@Value("${aliyun.sms.region-id}")private String regionId;public  R sendSms(String phoneNumber, Map<String, Object> templateParams) {try {DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);IAcsClient client = new DefaultAcsClient(profile);SendSmsRequest request = new SendSmsRequest();request.setPhoneNumbers(phoneNumber);request.setSignName(signName);request.setTemplateCode(templateCode);// 将HashMap转化为JSON字符串String templateParam = JSON.toJSONString(templateParams);request.setTemplateParam(templateParam);SendSmsResponse response = client.getAcsResponse(request);if(response.getCode() != null && response.getCode().equals("OK")){return R.ok();}return R.fail(response.getMessage());} catch (ClientException e) {e.printStackTrace();return null;}}}

3. 策略工厂与上下文

策略工厂负责管理和提供具体的策略实现:

package com.ruoyi.sms.handler;import com.ruoyi.sms.config.AliyunSmsUtil;
import com.ruoyi.system.api.constants.SmsType;
import com.ruoyi.sms.inter.SmsComInterFace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;/*** @Description:* @author: zh* @Create : 2025/4/30* @Project_name : RuoYi-Cloud* @Version :**/
@Component
@Slf4j
public class SmsTypeFactory {// 短信方式常量private static Map<String, SmsComInterFace> map = new ConcurrentHashMap<>();//上下文@Autowiredprivate ApplicationContext applicationContext;@PostConstructpublic void init()if(map.isEmpty()){//从上下文中找到所有实现了SmsComInterFace接口的类,并注册Map<String, SmsComInterFace> beansOfType = applicationContext.getBeansOfType(SmsComInterFace.class);beansOfType.forEach((k,v)->{map.put(k,v);});}}/*** 短信方式* @param type 传入短信方式* @return*/public static SmsComInterFace getSms(String type) {SmsComInterFace sms = null;if(map.containsKey(type)){sms = map.get(type);}if (sms == null) {throw new NullPointerException("方式选择错误");}return sms;}
}

策略模式的应用

在实际业务中使用短信服务:

@RestController
@RequestMapping("/sms")
public class SmsController {@GetMapping("/send/{phone}")public R sendSms(@RequestParam Map<String, Object> params, @PathVariable String phone,@RequestParam String type) {try {SmsComInterFace sms = SmsTypeFactory.getSms(type);return sms.sendSms(phone, params);} catch (IllegalArgumentException e) {return R.fail(e.getMessage());}}
}

策略模式的扩展性

当需要新增短信服务提供商时,只需:

  1. 实现SmsComInterFace接口
  2. 在工厂类中注册新策略
  3. 无需修改现有代码

例如新增腾讯云短信服务:

@Component
public class TencentSmsUtil implements SmsComInterFace {// 腾讯云实现...
}
//添加上Type
public class SmsType {public static final String TENCENT_SMS = "tencentSmsUtil";
}

策略模式的优势

  1. 开闭原则:无需修改现有代码即可扩展新策略

  2. 消除条件语句:避免大量的if-else或switch-case判断

  3. 易于测试:每个策略可以单独测试

  4. 运行时切换:可以根据配置动态切换策略

总结

通过策略模式设计短信服务架构,我们实现了:

  • 多种短信服务的统一接入
  • 业务代码与具体实现的解耦
  • 灵活的策略扩展能力
  • 便于维护和测试的代码结构

这种设计不仅适用于短信服务,也可以推广到支付网关、文件存储等需要支持多实现的场景。策略模式是保持软件扩展性和维护性的重要工具之一。

http://www.dtcms.com/wzjs/239913.html

相关文章:

  • 网站定时数据切换怎么做的网络销售
  • 怎么注册一个公司网站什么软件可以免费发广告
  • 乌鲁木齐网络公司班级优化大师下载
  • 企业网站建设流程第一步是什么安徽seo报价
  • 南山做网站价格网址域名ip查询
  • php企业网站无限制源码推广引流话术
  • 学做烘培的网站官方百度下载安装
  • 做外卖网站建站之星官网
  • 网站上在线订购怎么做苏州网站制作开发公司
  • 电子商务网站建设与综合实践软文什么意思范例
  • 网站建设后期费用东莞百度推广排名
  • 建设厅安全员c证网站优化推广培训
  • 情公司做的网站十堰seo排名公司
  • intitle 郑州网站建设长沙专业seo优化推荐
  • 石家庄做网站设计国外域名注册
  • 平价网站平价网站建设建设线上营销推广方式都有哪些
  • 美女做艾网站微网站
  • 公司网站制作哪家公司好广告推销网站
  • 社区网站建设网站关键词优化排名推荐
  • h5商城网站怎么做百度推广关键词质量度
  • 无代码编程软件上海最专业的seo公司
  • 网站安全评估怎么做百度com百度一下你
  • 手机网站建设软件郑州模板建站代理
  • 网站建设1000字海外品牌推广
  • wordpress产品选择厦门seo网站推广
  • 做电影平台网站怎么赚钱的推广普通话手抄报模板
  • 做的好的中医网站谷歌seo网络公司
  • 珠海建站模板搭建比较好的友链平台
  • 什么主题和风格的网站好如何能查到百度搜索排名
  • 设计本科郑州厉害的seo优化顾问