This repository has been archived on 2021-09-07. You can view files and clone it, but cannot push or open issues or pull requests.

52 lines
1.1 KiB
Go
Raw Permalink Normal View History

2018-01-03 23:35:09 +03:00
package adapter
2019-08-12 15:50:16 +02:00
//old, bad service which we can't change
2018-01-03 23:35:09 +03:00
type OldEmailSender interface {
Send(from, to, subject, body string) error
}
type OldEmailSvc struct{}
func (s OldEmailSvc) Send(from, to, subject, body string) error {
//Send email...
return nil
}
2019-08-12 15:50:16 +02:00
//new service which has state
//we have a client only supporting this interface
2018-01-03 23:35:09 +03:00
type NewEmailSender interface {
Send() error
}
type NewEmailService struct {
From, To, Subject, Body string
}
func (s NewEmailService) Send() error {
//state already initialized just send
return nil
}
2019-08-12 15:50:16 +02:00
//adapter for the old email service
2018-01-03 23:35:09 +03:00
type OldEmailServiceAdapter struct {
From, To, Subject, Body string
OldEmailService OldEmailSender
}
func (a OldEmailServiceAdapter) Send() error {
return a.OldEmailService.Send(a.From, a.To, a.Subject, a.Body)
}
/*
2019-08-12 15:50:16 +02:00
client which only supports the new email service interface
2018-01-03 23:35:09 +03:00
EmailClient{ Mail: OldEmailServiceAdapter{...}} old service
EmailClient{ Mail: NewEmailService{...}} new service
*/
type EmailClient struct {
Mail NewEmailSender
}
func (e EmailClient) SendEmail(From, To, Subject, Body string) error {
return e.Mail.Send()
}