Skip to content

Instantly share code, notes, and snippets.

@SiarheiFedartsou
Created December 4, 2017 17:44
Show Gist options
  • Save SiarheiFedartsou/2e8ef9e09a26c6fc172ca26b8c3a2bb8 to your computer and use it in GitHub Desktop.
Save SiarheiFedartsou/2e8ef9e09a26c6fc172ca26b8c3a2bb8 to your computer and use it in GitHub Desktop.
protocol ShapeVisitor {
func visit(_ rectangle: Rectangle)
func visit(_ circle: Circle)
}
protocol Shape {
func accept(_ visitor: ShapeVisitor)
}
struct Rectangle: Shape {
let width: Float
let height: Float
func accept(_ visitor: ShapeVisitor) {
visitor.visit(self)
}
}
struct Circle: Shape {
let radius: Float
func accept(_ visitor: ShapeVisitor) {
visitor.visit(self)
}
}
final class SquareShapeVisitor: ShapeVisitor {
var square: Float = 0
func visit(_ rectangle: Rectangle) {
square = rectangle.width * rectangle.height
}
func visit(_ circle: Circle) {
square = circle.radius * circle.radius * Float.pi
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment