Created
September 2, 2009 14:17
-
-
Save jeppenejsum/179733 to your computer and use it in GitHub Desktop.
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
trait PaymentComponent { | |
val paymentService: PaymentService | |
trait PaymentService {def pay(what: String) } | |
} | |
trait PaypalPaymentService extends PaymentComponent { | |
override val paymentService:PaymentService = new PaypalImpl | |
class PaypalImpl extends PaymentService { | |
def pay(what: String) = println("Paypal payment for "+what) | |
} | |
} | |
trait CyberSourcePaymentService extends PaymentComponent { | |
override val paymentService:PaymentService = new CyberSourceImpl | |
class CyberSourceImpl extends PaymentService { | |
def pay(what: String) = println("CyberSource payment for "+what+ " in "+this) | |
} | |
} | |
trait ServiceWithPaymentComponent extends PaymentComponent { | |
val depService: ServiceWithPayment | |
trait ServiceWithPayment { | |
def impl:String // Abstract, must implement | |
def doit = {println("Doing it with "+impl+" and paying it"); paymentService.pay(impl)} | |
} | |
} | |
trait MyServiceWithPayment extends ServiceWithPaymentComponent with PaymentComponent { | |
override val depService = new MyServiceWithPaymentImpl | |
class MyServiceWithPaymentImpl extends ServiceWithPayment { | |
def impl = "MyDepImpl" | |
} | |
} | |
trait Configuration extends CyberSourcePaymentService with MyServiceWithPayment { | |
} | |
trait MockConfiguration extends PaymentComponent with MyServiceWithPayment { | |
class MockImpl extends PaymentService { | |
def pay(what: String) = println("Mock payment for "+what+ " in "+this) | |
} | |
override val paymentService = new MockImpl | |
} | |
class MySnippet extends PaymentComponent with Configuration { | |
def render = { println("Rendering my snippet"); paymentService.pay("my stuff")} | |
} | |
class YourSnippet extends PaymentComponent with ServiceWithPaymentComponent with Configuration { | |
def render = {println("Rendering your snippet"); depService.doit } | |
} | |
object Scratch { | |
def main(args : Array[String]) : Unit = { | |
// Real config | |
(new MySnippet).render | |
(new YourSnippet).render | |
// Do test | |
(new MySnippet with MockConfiguration).render | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment