I'm going to use Chain of responsibility pattern for simple CLI dialog:
type Handler interface {
Request(flag bool)
}
type AskName struct {
next Handler
}
func (h *AskName) Request(flag bool) {
fmt.Println("AskName.Request()")
if flag {
h.next.Request(flag)
}
}
type AskAge struct {
next Handler
}
func (h *AskAge) Request(flag bool) {
fmt.Println("AskAge.Request()")
if flag {
h.next.Request(flag)
}
}
type AskEmail struct {
next Handler
}
func (h *AskEmail) Request(flag bool) {
fmt.Println("AskEmail.Request()")
}
func main() {
handlerA := &AskName{&AskAge{new(AskEmail)}}
handlerA.Request(true)
}
Question: imagine situation, when user entered invalid email. How can I re-call AskEmail
handler (or call other handler at all)?
For example, if I split logic like this:
type AskEmail struct {
next Handler
}
func (h *AskEmail) Request(flag bool) {
fmt.Println("AskEmail.Request()")
if flag {
h.next.Request(flag)
}
}
type ValidateEmail struct {
next Handler
}
func (h *ValidateEmail) Request(flag bool) {
fmt.Println("ValidateEmail.Request()")
}
how can I call AskEmail
from ValidateEmail
if ValidateEmail
fails?
I think you can do this
type AskEmail struct {
next Handler
}
func (h *AskEmail) Request(flag bool) {
fmt.Println("AskEmail.Request()")
if flag {
h.next.Request(flag)
}
}
type ValidateEmail struct {
next Handler
prev Handler
}
func (h *ValidateEmail) Request(flag bool) {
fmt.Println("ValidateEmail.Request()")
if !valid {
h.prev.Request(flag)
}
}
askEmail := &AskEmail{}
validateEmail := &ValidateEmail{prev: askEmail}
askEmail.next = validateEmail
imagine situation, when user entered invalid email. How can I re-call
AskEmail
handler (or call other handler at all)?
In my opinion, the AskEmail
"link" in the chain shouldn't call next
unless it got back a valid e-mail address. Each "link" should only call next
if its own responsibility has been handled - if you look at both https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern#UML_class_and_sequence_diagram and https://refactoring.guru/design-patterns/chain-of-responsibility you will see control only travels in one direction.
The example at https://github.com/yksz/go-design-patterns/blob/master/behavior/chain_of_responsibility.go is a bit simplistic
If you follow this idea, your code will become something like this:
func main() {
// Build a chain of steps to take
c := AskEmail{
next: AskName{
next: Print{},
},
}
// Run the chain with an empty "Person"
c.Run(&Person{})
}
// Person holds the data that is relevant to your application. It should have a name that makes sense for your domain
type Person struct {
Email string
Name string
}
// PersonInfoChainLink is a link in the chain of command
type PersonInfoChainLink interface {
Run(p *Person) (error)
}
// AskEmail can ask for an e-mail address
type AskEmail struct {
next PersonInfoChainLink
}
func (a AskEmail) Run(p *Person) (error) {
// Ask for e-mail addresses until the user gives a valid one
var err error
var email string
for {
email, err = askString(`What is your e-mail address?`)
if err != nil {
return err
}
if strings.Contains(email, `@`) {
break
}
fmt.Printf("Invalid e-mail address %s!", email)
}
p.Email = email
return a.next.Run(p)
}
// AskName can ask for the name of a person
type AskName struct {
next PersonInfoChainLink
}
func (a AskName) Run(p *Person) (error) {
name, err := askString(`What is your name?`)
if err != nil {
return err
}
p.Name = name
return a.next.Run(p)
}
// Print can print the information about a person
type Print struct {
}
func (Print) Run(p *Person) (error) {
log.Printf(`Email %s at %s!`, p.Name, p.Email)
return nil
}
func askString(question string) (string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print(question + ` `)
return reader.ReadString('
')
}