<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 概述 先通過抽象方式實現步驟,在通過具體類替換抽象接口 ## 示例 ### 一次性密碼功能 讓我們來考慮一個一次性密碼功能(OTP)的例子。將 OTP 傳遞給用戶的方式多種多樣(短信、郵件等)。但無論是短信還是郵件,整個 OTP 流程都是相同的: 1. 生成隨機的 n 位數字。 2. 在緩存中保存這組數字以便進行后續驗證。 3. 準備內容。 4. 發送通知。 5. 發布。 <details> <summary>main.go</summary> ``` package main import "fmt" // 模板方法 type iOtp interface { genRandomOTP(int) string saveOTPCache(string) getMessage(string) string sendNotification(string) error publishMetric() } // 方式一:在調用時候選擇 // type otp struct { // } // func (o *otp) genAndSendOTP(iOtp iOtp, otpLength int) error { // otp := iOtp.genRandomOTP(otpLength) // iOtp.saveOTPCache(otp) // message := iOtp.getMessage(otp) // err := iOtp.sendNotification(message) // if err != nil { // return err // } // iOtp.publishMetric() // return nil // } // 方式二 type otp struct { iOtp iOtp } func (o *otp) genAndSendOTP(otpLength int) error { otp := o.iOtp.genRandomOTP(otpLength) o.iOtp.saveOTPCache(otp) message := o.iOtp.getMessage(otp) err := o.iOtp.sendNotification(message) if err != nil { return err } o.iOtp.publishMetric() return nil } // 具體實施 type sms struct { otp } func (s *sms) genRandomOTP(len int) string { randomOTP := "1234" fmt.Printf("SMS: generating random otp %s\n", randomOTP) return randomOTP } func (s *sms) saveOTPCache(otp string) { fmt.Printf("SMS: saving otp: %s to cache\n", 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 } func (s *sms) publishMetric() { fmt.Printf("SMS: publishing metrics\n") } // 具體實施 type email struct { otp } func (s *email) genRandomOTP(len int) string { randomOTP := "1234" fmt.Printf("EMAIL: generating random otp %s\n", randomOTP) return randomOTP } func (s *email) saveOTPCache(otp string) { fmt.Printf("EMAIL: saving otp: %s to cache\n", 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 } func (s *email) publishMetric() { fmt.Printf("EMAIL: publishing metrics\n") } func main() { // 方式一 // otp := otp{} // smsOTP := &sms{ // otp: otp, // } // smsOTP.genAndSendOTP(smsOTP, 4) // emailOTP := &email{ // otp: otp, // } // emailOTP.genAndSendOTP(emailOTP, 4) // fmt.Scanln() // 方式一 smsOTP := &sms{} o := otp{ iOtp: smsOTP, } o.genAndSendOTP(4) fmt.Println("") emailOTP := &email{} o = otp{ iOtp: emailOTP, } o.genAndSendOTP(4) } ``` </details> <br /> 輸出 ``` MS: generating random otp 1234 SMS: saving otp: 1234 to cache SMS: sending sms: SMS OTP for login is 1234 SMS: publishing metrics EMAIL: generating random otp 1234 EMAIL: saving otp: 1234 to cache EMAIL: sending email: EMAIL OTP for login is 1234 EMAIL: publishing metrics ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看