Created
January 31, 2025 23:20
-
-
Save bartekpacia/84827bb98ed0389bf65e73a7a8aae12f 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
abstract class Pastry { | |
abstract fun accept(visitor: PastryVisitor) | |
} | |
class Beignet : Pastry() { | |
override fun accept(visitor: PastryVisitor) { | |
visitor.visitBeignet(this) | |
} | |
} | |
class Cruller : Pastry() { | |
override fun accept(visitor: PastryVisitor) { | |
visitor.visitCruller(this) | |
} | |
} | |
/** | |
* Each operation that can be performed on pastries is a new class that implements that interface: | |
* - cooking | |
* - storing | |
*/ | |
interface PastryVisitor { | |
fun visitBeignet(beignet: Beignet) | |
fun visitCruller(cruller: Cruller) | |
} | |
fun main() { | |
val somePastry: Pastry = Beignet() | |
val visitor = BreakfastVisitor() | |
somePastry.accept(visitor) | |
} | |
// ------------ | |
class BreakfastVisitor : PastryVisitor { | |
override fun visitBeignet(beignet: Beignet) { | |
println("visiting beignet!!!") | |
} | |
override fun visitCruller(cruller: Cruller) { | |
println("visiting cruller!!!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment