Created
August 13, 2014 06:10
-
-
Save OleTraveler/04fe8eacd3326f250c9c to your computer and use it in GitHub Desktop.
Hibernate Inheritance and Using Pattern Matching
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
trait PaymentSource { | |
def accept[T](visitor: PsVisitor[T]) : T | |
} | |
class CreditCard extends PaymentSource{ | |
override def accept[T](visitor: PsVisitor[T]) = { | |
visitor.visit(this) | |
} | |
} | |
class Check extends PaymentSource{ | |
override def accept[T](visitor: PsVisitor[T]) = { | |
visitor.visit(this) | |
} | |
} | |
trait PsVisitor[T] { | |
def visit(cc: CreditCard) : T | |
def visit(ck: Check) : T | |
} | |
object PsFilter { | |
def apply(ps: PaymentSource) = { | |
ps.accept(new PsVisitor[PsResult] { | |
def visit(cc: CreditCard) = CreditCardResult(cc) | |
def visit(ck: Check) = CheckResult(ck) | |
}) | |
} | |
} | |
trait PsResult | |
case class CreditCardResult(cc: CreditCard) extends PsResult | |
case class CheckResult(cc: Check) extends PsResult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment