Last active
October 28, 2019 13:12
-
-
Save yubing24/fbbe36aaa19f0c3121338731dc6b6146 to your computer and use it in GitHub Desktop.
Template Method implementation using Go
This file contains 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 "fmt" | |
type CommonInterface interface { | |
Create() | |
Destroy() | |
} | |
type ConcreteA struct { | |
} | |
func (concrete ConcreteA) Create() { | |
fmt.Println("[A] Created!") | |
} | |
func (concrete ConcreteA) Destroy() { | |
fmt.Println("[A] Destroyed!") | |
} | |
type ConcreteB struct {} | |
func (concrete ConcreteB) Create() { | |
fmt.Println("[B] Created!") | |
} | |
func (concrete ConcreteB) Destroy() { | |
fmt.Println("[B] Destroyed!") | |
} | |
type Abstract struct { | |
Common CommonInterface | |
} | |
func (abstract Abstract) Run() { | |
fmt.Println("Do something before create") | |
abstract.Common.Create() | |
fmt.Println("Do something before destroy") | |
abstract.Common.Destroy() | |
fmt.Printf("Do something to wrap up\n\n") | |
} | |
func main() { | |
var a = ConcreteA{} | |
var c = Abstract{Common: a} | |
c.Run() | |
var b = ConcreteB{} | |
c.Common = b | |
c.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment