Created
February 21, 2012 10:51
-
-
Save kencoba/1875764 to your computer and use it in GitHub Desktop.
Adapter pattern (Design Patterns in Scala)
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 ProductPrice { | |
def price: Int | |
} | |
class Product { | |
var cost = 500 | |
} | |
class ProductDelegationAdapter extends ProductPrice { | |
private var product = new Product | |
override def price = product.cost | |
} | |
class ProductInheritanceAdapter extends Product with ProductPrice { | |
override def price = cost | |
} | |
object AdapterSample { | |
def main(args: Array[String]) = { | |
var product1 = new ProductDelegationAdapter | |
println(product1.price) | |
var product2 = new ProductInheritanceAdapter | |
println(product2.price) | |
} | |
} | |
AdapterSample.main(Array()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment