Created
December 4, 2017 17:44
-
-
Save SiarheiFedartsou/2e8ef9e09a26c6fc172ca26b8c3a2bb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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