Skip to main content

Go Example

package main

import (
"fmt"
)

type areaCalculator struct {
area int
}

func (a *areaCalculator) visitForSquare(s *square) {
// Calculate area for square.
// Then assign in to the area instance variable.
fmt.Println("Calculating area for square")
}

func (a *areaCalculator) visitForCircle(s *circle) {
fmt.Println("Calculating area for circle")
}
func (a *areaCalculator) visitForrectangle(s *rectangle) {
fmt.Println("Calculating area for rectangle")
}
package main

type circle struct {
radius int
}

func (c *circle) accept(v visitor) {
v.visitForCircle(c)
}

func (c *circle) getType() string {
return "Circle"
}
package main

import "fmt"

type middleCoordinates struct {
x int
y int
}

func (a *middleCoordinates) visitForSquare(s *square) {
// Calculate middle point coordinates for square.
// Then assign in to the x and y instance variable.
fmt.Println("Calculating middle point coordinates for square")
}

func (a *middleCoordinates) visitForCircle(c *circle) {
fmt.Println("Calculating middle point coordinates for circle")
}
func (a *middleCoordinates) visitForrectangle(t *rectangle) {
fmt.Println("Calculating middle point coordinates for rectangle")
}
package main

type rectangle struct {
l int
b int
}

func (t *rectangle) accept(v visitor) {
v.visitForrectangle(t)
}

func (t *rectangle) getType() string {
return "rectangle"
}
package main

type shape interface {
getType() string
accept(visitor)
}
package main

type square struct {
side int
}

func (s *square) accept(v visitor) {
v.visitForSquare(s)
}

func (s *square) getType() string {
return "Square"
}
package main

type visitor interface {
visitForSquare(*square)
visitForCircle(*circle)
visitForrectangle(*rectangle)
}
package main

import "fmt"

func main() {
square := &square{side: 2}
circle := &circle{radius: 3}
rectangle := &rectangle{l: 2, b: 3}

areaCalculator := &areaCalculator{}

square.accept(areaCalculator)
circle.accept(areaCalculator)
rectangle.accept(areaCalculator)

fmt.Println()
middleCoordinates := &middleCoordinates{}
square.accept(middleCoordinates)
circle.accept(middleCoordinates)
rectangle.accept(middleCoordinates)
}
Calculating area for square
Calculating area for circle
Calculating area for rectangle

Calculating middle point coordinates for square
Calculating middle point coordinates for circle
Calculating middle point coordinates for rectangle