From a4bcf12db6c5aaac9f8e7168ce01411869c13cd6 Mon Sep 17 00:00:00 2001 From: ismayilmalik Date: Tue, 16 Jan 2018 23:37:28 +0300 Subject: [PATCH] implemented template pattern --- behavioral/template_p/templateP.go | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 behavioral/template_p/templateP.go diff --git a/behavioral/template_p/templateP.go b/behavioral/template_p/templateP.go new file mode 100644 index 0000000..05919ca --- /dev/null +++ b/behavioral/template_p/templateP.go @@ -0,0 +1,41 @@ +package template_p + +import ( + "fmt" + +) + +type Greeter interface { + message() string +} + +type GreeterTemplate interface { + first() string + third() string + greet(Greeter) string +} + +type PrincesGreeterTmpl struct {} +func (p PrincesGreeterTmpl) first() string { + return "Welcome" +} +func (p PrincesGreeterTmpl) third() string { + return "to our palace!" +} +func (p PrincesGreeterTmpl) greet(g Greeter) string { + return fmt.Sprintf("%s, %s %s", p.first(), g.message(), p.third()) +} + +type PrincesGreeter struct { + name string +} +func (p *PrincesGreeter) message() string { + return fmt.Sprintf("your magesty princes %s", p.name) +} + +func Demo() { + prGreeter := &PrincesGreeter{"Diana"} + tmpl := PrincesGreeterTmpl{} + + fmt.Println(tmpl.greet(prGreeter)) +} \ No newline at end of file