Created
December 2, 2011 23:04
-
-
Save dcadenas/1425250 to your computer and use it in GitHub Desktop.
"Template method" in 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 | |
type IChild interface { | |
Describe() | |
PrintName() | |
} | |
type Base struct { child IChild } | |
func (b *Base)Describe(){ | |
println("I'm a child of Base") | |
b.child.PrintName() | |
} | |
//The template method | |
func (b *Base)PrintName(){ | |
println(" Nobody gave me a name :(") | |
} | |
type Child1 struct { *Base } | |
func (c Child1)PrintName() { | |
println(" My name is Child1!") | |
} | |
func newChild1() (child IChild) { | |
child = Child1{new(Base)} | |
child.(Child1).child = child | |
return | |
} | |
type Child2 struct { *Base } | |
func newChild2() (child IChild) { | |
child = Child2{new(Base)} | |
child.(Child2).child = child | |
return | |
} | |
func main() { | |
var child1 = newChild1() | |
child1.Describe() | |
var child2 = newChild2() | |
child2.Describe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment