Template Pattern
The template method design pattern allows the different parts of the object to be abstracted, executing the same logic in the same piece of code, and increasing extensibility. In the go language, implementations are implemented by the underlying object, and the behavior is controlled by the top-level method. Implement
Package template
Import "FMT"
type Tmpl interface{
dosomething ()
doanything ()
}
type Tmpla struct{
}
func (this *tmpla) dosomething () {
fmt. Println ("tmpla.dosomething")
}
func (this *tmpla) doanything () {
fmt. Println ("tmpla.doanything")
}
type TMPLB struct{
}
func (this *tmplb) dosomething () {
fmt. Println ("tmplb.dosomething")
}
func (this *tmplb) doanything () {
fmt. Println ("tmplb.doanything")
}
func Operate (Tmpl tmpl) {
Tmpl. DoSomething ()
Tmpl. Doanything ()
}
Use
Package main
Import (
"Projects/designpatternsbygo/behavioralpatterns/template"
)
func main () {
A: = template. tmpla{}
B: = template. tmplb{}
template. Operate (&a)
template. Operate (&b)
} Money to hold
a court, no money in the holding of personal field.
It's not easy to come out.