Skip to content

Instantly share code, notes, and snippets.

@yubing24
Last active October 28, 2019 13:12
Show Gist options
  • Save yubing24/fbbe36aaa19f0c3121338731dc6b6146 to your computer and use it in GitHub Desktop.
Save yubing24/fbbe36aaa19f0c3121338731dc6b6146 to your computer and use it in GitHub Desktop.
Template Method implementation using Go
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