Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created February 21, 2012 10:51
Show Gist options
  • Save kencoba/1875764 to your computer and use it in GitHub Desktop.
Save kencoba/1875764 to your computer and use it in GitHub Desktop.
Adapter pattern (Design Patterns in Scala)
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