Skip to content

Instantly share code, notes, and snippets.

@LH17
Created January 1, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LH17/ab4c7c957bf7f50ffa8729e4365ab6f4 to your computer and use it in GitHub Desktop.
Save LH17/ab4c7c957bf7f50ffa8729e4365ab6f4 to your computer and use it in GitHub Desktop.
Visitor Design Pattern
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