From 43ba305859d54195b97fb62f5da585806b96cc44 Mon Sep 17 00:00:00 2001 From: ismayilmalik Date: Mon, 15 Jan 2018 23:47:45 +0300 Subject: [PATCH] implemented visitor pattern --- behavioral/visitor/visitor.go | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 behavioral/visitor/visitor.go diff --git a/behavioral/visitor/visitor.go b/behavioral/visitor/visitor.go new file mode 100644 index 0000000..5773186 --- /dev/null +++ b/behavioral/visitor/visitor.go @@ -0,0 +1,40 @@ +package visitor + +import ( + "fmt" +) + +type Visitor interface { + VisitProductWithHighTask(ProductWithHighTax) float32 + VisitProductWithLowTask(ProductWithLowTax) float32 +} + +type Visitable interface { + Accept(Visitor) float32 +} + +type TaxVisitor struct {} +func (v TaxVisitor) VisitProductWithHighTask(p ProductWithHighTax) float32 { + fmt.Println("Visiting Product With High Tax ...") + return p.Price * 1.3 +} + +func (v TaxVisitor) VisitProductWithLowTask(p ProductWithLowTax) float32 { + fmt.Println("Visiting Product With Low Tax ...") + return p.Price * 1.1 +} + + +type ProductWithHighTax struct { + Price float32 +} +func (p ProductWithHighTax) Accept(v Visitor) float32 { + return v.VisitProductWithHighTask(p) +} + +type ProductWithLowTax struct{ + Price float32 +} +func (p ProductWithLowTax) Accept(v Visitor) float32 { + return v.VisitProductWithLowTask(p) +} \ No newline at end of file