Skip to content

Instantly share code, notes, and snippets.

@lotusirous
Created March 8, 2019 00:54
Show Gist options
  • Select an option

  • Save lotusirous/7879979b29f52c7a0c2bd1f8c8c809d5 to your computer and use it in GitHub Desktop.

Select an option

Save lotusirous/7879979b29f52c7a0c2bd1f8c8c809d5 to your computer and use it in GitHub Desktop.
Implement proxy pattern in golang
package main
import (
"log"
)
type Wizard struct {
Name string
}
type WizardTower interface {
Enter(w Wizard)
}
type IvyTower struct{}
func (i IvyTower) Enter(w Wizard) {
log.Printf("%s enter tower", w.Name)
}
// ProxyTower implementation
type ProxyTower struct {
Tower WizardTower
NUMBER_OF_WIZARD int8
}
// Enter applies proxy policy
func (proxy *ProxyTower) Enter(w Wizard) {
if proxy.NUMBER_OF_WIZARD < 2 {
proxy.Tower.Enter(w)
proxy.NUMBER_OF_WIZARD++
} else {
log.Printf("Number of wizard is limited")
}
}
func main() {
log.Printf("hello world")
p := ProxyTower{Tower: IvyTower{}}
p.Enter(Wizard{Name: "Red wizard"})
p.Enter(Wizard{Name: "White wizard"})
p.Enter(Wizard{Name: "Black wizard"})
p.Enter(Wizard{Name: "Brown wizard"})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment