Created
March 8, 2019 00:54
-
-
Save lotusirous/7879979b29f52c7a0c2bd1f8c8c809d5 to your computer and use it in GitHub Desktop.
Implement proxy pattern in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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