Go 模板办法形式讲解和代码示例
模版办法是一种行为设计形式, 它在基类中定义了一个算法的框架, 允许子类在不修改结构的状况下重写算法的特定步骤。
概念示例
让咱们来考虑一个一次性暗码功能 (OTP) 的比如。 将 OTP 传递给用户的办法多种多样 (短信、 邮件等)。 但无论是短信仍是邮件, 整个 OTP 流程都是相同的:
- 生成随机的 n 位数字。
- 在缓存中保存这组数字以便进行后续验证。
- 准备内容。
- 发送告诉。
后续引进的任何新 OTP 类型都很有可能需要进行相同的上述步骤。
因而, 咱们会有这样的一个场景, 其间某个特定操作的步骤是相同的, 但完成办法却可能有所不同。 这正是适合考虑使用模板办法形式的状况。
首先, 咱们定义一个由固定数量的办法组成的根底模板算法。 这就是咱们的模板办法。 然后咱们将完成每一个步骤办法, 但不会改动模板办法。
otp.go: 模板办法
package main
type IOtp interface {
genRandomOPT(int) string
saveOPTCache(string)
getMessage(string) string
sendNotification(string) error
}
type Otp struct {
iOtp IOtp
}
func (o *Otp) genAndSendOPT(optLength int) error {
opt := o.iOtp.genRandomOPT(optLength)
o.iOtp.saveOPTCache(opt)
message := o.iOtp.getMessage(opt)
if err := o.iOtp.sendNotification(message); err != nil {
return err
}
return nil
}
sms.go: 详细施行
package main
import (
"fmt"
)
type Sms struct {
Otp
}
func (s *Sms) genRandomOPT(len int) string {
randomOTP := "1234"
fmt.Printf("SMS: generating random otp %s \n", randomOTP)
return randomOTP
}
func (s *Sms) saveOPTCache(otp string) {
fmt.Printf("SMS: saving otp %s", otp)
}
func (s *Sms) getMessage(otp string) string {
return "SMS OTP for login is " + otp
}
func (s *Sms) sendNotification(message string) error {
fmt.Printf("SMS: sending sms: %s\n", message)
return nil
}
email.go: 详细施行
package main
import (
"fmt"
)
type Email struct {
Otp
}
func (s *Email) genRandomOPT(len int) string {
randomOTP := "2345"
fmt.Printf("Email: generating random otp %s \n", randomOTP)
return randomOTP
}
func (s *Email) saveOPTCache(otp string) {
fmt.Printf("Email: saving otp %s to cache", otp)
}
func (s *Email) getMessage(otp string) string {
return "Email otp for login is " + otp
}
func (s *Email) sendNotification(message string) error {
fmt.Printf("Email: sending email %s \n", message)
return nil
}
main.go: 客户端代码
package main
import "fmt"
func main() {
smsOTP := &Sms{}
o := Otp{
iOtp: smsOTP,
}
o.genAndSendOPT(4)
fmt.Println("")
emailOtp := &Email{}
o = Otp{
iOtp: emailOtp,
}
o.genAndSendOPT(4)
}
output.txt: 履行结果
SMS: generating random otp 1234
SMS: saving otp 1234SMS: sending sms: SMS OTP for login is 1234
Email: generating random otp 2345
Email: saving otp 2345 to cacheEmail: sending email Email otp for login is 2345