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

发送信息----策略模式

发送信息----策略模式

  • 发送信息

发送信息

发送信息到手机、邮箱等,可扩展

package mainimport ("errors""fmt"
)type PushContext struct {Phone, Email, Message stringTage                  int
}type PaymentStrategy interface {Push(*PushContext)
}func NewPush(phone, email, message string, tage int, strategy PaymentStrategy) *Push {return &Push{Content: &PushContext{Phone:   phone,Email:   email,Message: message,Tage:    tage,},Strategy: strategy,}
}type Push struct {Content  *PushContextStrategy PaymentStrategy
}func (p *Push) Push() {p.Strategy.Push(p.Content)
}//==================================================================================================================type PushPhone struct {Content stringPhone   string
}func (p *PushPhone) Push(ctx *PushContext) {fmt.Printf("发送到手机:Send Message %s to phone %s by cash\n", ctx.Message, ctx.Phone)
}type PushEmail struct {Content stringEmail   string
}func (p *PushEmail) Push(ctx *PushContext) {fmt.Printf("发送到邮箱:Send Message %s to Email %s by cash\n", ctx.Message, ctx.Email)
}var sendMethod = make(map[string]PaymentStrategy)
func init() {sendMethod["PushPhone"] = &PushPhone{}sendMethod["PushEmail"] = &PushEmail{}
}func main() {pushMessage("13301161111", "", "这是发给手机的短信", "PushPhone", 1)pushMessage("", "931@qq.com", "这是发给邮箱的邮件", "PushEmail", 1)}func pushMessage(phone, email, message, sty string, tage int) error {//查询信息,得到phone、email、发送标识等strategy, ok := sendMethod[sty]if !ok {return errors.New("没有这个策略")}payment := NewPush(phone, email, message, tage, strategy)payment.Push()return nil
}
http://www.lryc.cn/news/103854.html

相关文章:

  • PySpark介绍与安装
  • 细讲TCP三次握手四次挥手(三)
  • vue 组件中 data 为什么必须是函数
  • 从零开始学python(十二)如何成为一名优秀的爬虫工程师
  • 为高性能计算构建,由亚马逊云科技Amazon Graviton3E驱动的最新实例正式可用
  • BUUCTF题目Crypto部分wp(持续更新)
  • A Generalized Loss Function for Crowd Counting and Localization阅读笔记
  • SocketD协议单链接双向RPC模式怎么实现
  • apache poi 设置背景颜色
  • Vue2-Vue3组件间通信-EventBus方式-函数封装
  • 【SpringBoot】| SpringBoot 和 web组件
  • dflow工作流使用1——架构和基本概念
  • python小游戏课程设计报告,python游戏课程设计报告
  • 使用Windbg分析从系统应用程序日志中找到的系统自动生成的dump文件去排查问题
  • 后端技术趋势指南|如何选择自己的技术方向
  • Delphi XE的原生JSONObject如何判断键值是否存在?
  • Go Runtime功能初探
  • 01|Oracle学习(监听程序、管理工具、PL/SQL Developer、本地网络服务介绍)
  • 滴滴数据服务体系建设实践
  • VBA技术资料MF36:VBA_在Excel中排序
  • Shell脚本学习3
  • 代理模式--静态代理和动态代理
  • C++容器——list的模拟实现
  • VUE3 祖孙组件传值调用方法
  • 我的网安之路
  • langchain-ChatGLM源码阅读:webui.py
  • <C++>二、 类和对象
  • 【HttpRunnerManager】搭建接口自动化测试平台实战
  • 【adb】adb常用命令
  • SAP 委外副产品业务