Created
August 16, 2009 19:57
-
-
Save chrislewis/168742 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
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