Created
January 1, 2019 15:28
-
-
Save LH17/ab4c7c957bf7f50ffa8729e4365ab6f4 to your computer and use it in GitHub Desktop.
Visitor Design Pattern
This file contains 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 Country { | |
func accept(visitor: CountryVisitor) | |
} | |
protocol CountryVisitor { | |
func visit(country: India) | |
func visit(country: Brazil) | |
func visit(country: China) | |
} | |
class India: Country { | |
func accept(visitor: CountryVisitor) { | |
visitor.visit(country: self) | |
} | |
} | |
class Brazil: Country { | |
func accept(visitor: CountryVisitor) { | |
visitor.visit(country: self) | |
} | |
} | |
class China: Country { | |
func accept(visitor: CountryVisitor) { | |
visitor.visit(country: self) | |
} | |
} | |
class CountryVisitorName: CountryVisitor { | |
var visitorName = "" | |
func visit(country: India) { | |
visitorName = "Chen" | |
} | |
func visit(country: Brazil) { | |
visitorName = "Ramesh" | |
} | |
func visit(country: China) { | |
visitorName = "Antonio" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment