Go語言程式:建立名為 Mailer 的介面,定義 Send 方法


本文將介紹如何建立一個名為 mailer 的介面,該介面使用介面嵌入和函式作為引數來定義 send 方法。Go 語言中的介面是定義一組行為的方法集合。

演算法

建立一個 Mailer 介面,其中包含一個 Send 函式,該函式接受兩個引數:收件人的電子郵件地址和電子郵件正文。如果傳送過程中發生錯誤,Send 方法應返回錯誤。

示例 1

  • 建立表示各種郵件傳送程式實現的結構體,例如 SmtpMailer、SendGridMailer 等。

  • 根據各自的郵件傳送邏輯,為每個郵件傳送程式結構體實現 Send 函式。

  • 建立一個名為 MailerFacade 的新結構體,該結構體具有 Mailer 介面。

  • 此型別用作多個郵件傳送程式實現的容器。

  • 在主程式中建立郵件傳送程式實現的例項,並將其包裝在 MailerFacade 例項中。

  • 將 MailerFacade 例項儲存在 Mailer 介面切片中。遍歷切片,使用收件人的電子郵件地址和電子郵件正文作為引數,在每個郵件傳送程式上呼叫 Send 方法。

示例 2

  • 建立描述各種郵件傳送或寫入邏輯的函式,例如 SendSMTPMail、SendSendGridMail 等。

  • 檢查這些方法是否與 Mailer 介面的 Send 方法具有相同的簽名。

  • 在主程式中建立一個函式切片,每個函式都與 Send 方法具有相同的簽名。

  • 用反映各種郵件傳送或寫入邏輯的函式填充切片。

  • 遍歷切片,使用收件人的電子郵件地址和電子郵件正文作為引數呼叫每個函式。

示例 1

此示例允許介面嵌入其他介面,使嵌入的介面繼承嵌入介面的所有方法。下面的程式碼定義了多個滿足 mailer 介面的郵件傳送程式實現。主函式建立郵件傳送程式的例項,並用一個外觀模式包裝它們,該外觀模式迭代一個 mailer 函式切片來發送郵件。

package main

import "fmt"

type Mailer interface {
   Send(string, string) error
}

type SmtpMailer struct{}

func (s *SmtpMailer) Send(to, body string) error {
   fmt.Printf("Sending SMTP mail to: %s\nBody: %s\n", to, body)
   return nil
}

type SendGridMailer struct{}

func (s *SendGridMailer) Send(to, body string) error {
   fmt.Printf("Sending SendGrid mail to: %s\nBody: %s\n", to, body)
   return nil
}

type FileMailer struct{}

func (f *FileMailer) Send(to, body string) error {
   fmt.Printf("Writing mail to file for: %s\nBody: %s\n", to, body)
   return nil
}

type MailerFacade struct {
   Mailer
}

func main() {
   smtpMailer := &SmtpMailer{}
   sendGridMailer := &SendGridMailer{}
   fileMailer := &FileMailer{}

   smtpMailerFacade := MailerFacade{smtpMailer}
   sendGridMailerFacade := MailerFacade{sendGridMailer}
   fileMailerFacade := MailerFacade{fileMailer}

   mailers := []Mailer{&smtpMailerFacade, &sendGridMailerFacade, &
fileMailerFacade}

   for _, mailer := range mailers {
      err := mailer.Send("emil@example.com", "Hello, World!")
      if err != nil {
         fmt.Printf("Error sending mail: %v\n", err)
      }
   }
}

輸出

Sending SMTP mail to: email@example.com 
Body: Hello, World! 
Sending SendGrid mail to: email@example.com 
Body: Hello, World! 
Writing mail to file for: email@example.com
Body: Hello, World!

示例 2

在此示例中,我們將使用 Go 語言中的函式切片來選擇不同的郵件傳送實現。透過此方法,我們可以輕鬆地在不同的郵件傳送技術之間切換。下面的程式碼定義了表示不同郵件傳送程式的多個函式,以及一個主函式,該函式迭代一個郵件傳送程式函式切片並呼叫每個函式以將傳送郵件寫入檔案。

package main

import "fmt"

type Mailer interface {
   Send(to, body string) error
}

func SendSMTPMail(to, body string) error {
   fmt.Printf("Sending SMTP mail to: %s\nBody: %s\n", to, body)
   return nil
}

func SendSendGridMail(to, body string) error {
   fmt.Printf("Sending SendGrid mail to: %s\nBody: %s\n", to, body)
   return nil
}

func WriteMailToFile(to, body string) error {
   fmt.Printf("Writing mail to file for: %s\nBody: %s\n", to, body)
   return nil
}

func main() {
   mailers := []func(string, string) error{
      SendSMTPMail,
      SendSendGridMail,
      WriteMailToFile,
   }

   for _, mailer := range mailers {
      err := mailer("email@example.com", "Hello, World!")
      if err != nil {
         fmt.Printf("Error sending mail: %v\n", err)
      }
   }
}

輸出

Sending SMTP mail to: email@example.com 
Body: Hello, World! 
Sending SendGrid mail to: email@example.com 
Body: Hello, World! 
Writing mail to file for: email@example.com
Body: Hello, World!

結論

本文討論瞭如何使用介面建立一個名為 mailer 的介面,該介面定義了一個 send 方法,透過使用介面,我們為不同的郵件傳送程式實現定義了一個公共契約。這裡我們討論了兩種方法,包括嵌入介面和主方法。

更新於:2023年7月13日

69 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.