Advertisement
Guest User

Untitled

a guest
Oct 28th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.92 KB | None | 0 0
  1. /** Account */
  2. class Account extends ConsoleLogger with ShortLogger with TimestampLogger {
  3.   private var balance = 0
  4.   log("Account created")
  5.  
  6.   def deposit(amount: Int) {
  7.     balance += amount
  8.     log("deposit of " + amount + ". new balance: " + balance)
  9.   }
  10. }
  11.  
  12. /** Logger */
  13. trait Logger {
  14.   def log(msg: String) // abstract method
  15. }
  16.  
  17. trait ConsoleLogger extends Logger {
  18.   def log(msg: String) { // concrete method
  19.     println(msg)
  20.   }
  21. }
  22.  
  23. trait TimestampLogger extends Logger {
  24.   // abstract for it might call super[Logger].log
  25.   abstract override def log(msg: String) {
  26.     super.log(new java.util.Date() + " " + msg)
  27.   }
  28. }
  29.  
  30. trait ShortLogger extends Logger {
  31.   val maxLength = 15
  32.   // abstract for it might call super[Logger].log
  33.   abstract override def log(msg: String) {
  34.     super.log(
  35.         if (msg.length <= maxLength) msg
  36.         else msg.take(maxLength - 3) + "..."
  37.         )
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement