Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created August 16, 2009 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/168742 to your computer and use it in GitHub Desktop.
Save chrislewis/168742 to your computer and use it in GitHub Desktop.
case class Employee(val dailyRate: Double) {
def getDailyRate = dailyRate
}
trait Calculator {
def calculate(dailyRate: Double, noOfDays: Int): Double
}
trait Calendar {
def noOfDays: Int
}
abstract class SalaryCalculationEngine {
trait Context {
val calculator: Calculator;
val calendar: Calendar;
}
protected val ctx: Context
type E <: Employee
def calculate(dailyRate: Double): Double = {
ctx.calculator.calculate(dailyRate, ctx.calendar.noOfDays)
}
def payroll(employees: List[E]) = {
employees.map(_.getDailyRate).foreach(s => println(calculate(s)))
}
}
trait Class1SalaryConfig {
val calculator = new DefaultCalculator
val calendar = new DefaultCalendar
class DefaultCalculator extends Calculator {
def calculate(basic: Double, noOfDays: Int): Double = basic * noOfDays
}
class DefaultCalendar extends Calendar {
def noOfDays: Int = 30
}
}
object App {
def main(args: Array[String]) {
val emps = List(Employee(500d))
val sal = new SalaryCalculationEngine {
type E = Employee
protected val ctx = new Class1SalaryConfig with Context
}
sal.payroll(emps)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment