Decorator Pattern In Scala - No Fluff Just Stuff

Decorator Pattern In Scala

Posted by: Daniel Hinojosa on November 24, 2009

As part of the Scala Study Group in Albuquerque, we had an assignment to reproduce some of the popular design patterns in Scala. Although the assignment is over, it is worth while to log about it. The following pattern was taken from Groovy Decorator Example.


trait Logger {
  def log(message: String) = {
    message
  }
}

Figure 1: Establishing a trait shared by all Loggers.


object StdLogger extends Logger

Figure 2: Objects are explicit creations of an object. There are no statics in Scala, and for good reason. Reasons that are too many for me to cover here. What this does is create an object called StdLogger. This is merely an implementation of the trait with no added behavior.


class TimeStampingLogger(logger: Logger) extends Logger {
  override def log(message: String) = {
    val now = Calendar.getInstance
    now.getTime.toString + ' ' + logger.log(message)
  }
}

Figure 3: The time stamping logger takes a Logger object and decorates with the current time prepended with whatever message is returned from the parent logger.


class UpperLogger(logger: Logger) extends Logger {
  override def log(message: String) = {
    logger.log(message).toUpperCase
  }
}

Figure 4: The UpperLogger will capitalize all letters returned from the parent Logger object.


object DecoratorRunner extends Application {
  override def main(args: Array[String]) {
    val timeStampingLogger = new TimeStampingLogger(StdLogger)
    val upperLogger = new UpperLogger(timeStampingLogger)
    println(upperLogger.log(StdLogger.log("Decorator for the Scala Study Group")))
  }
}

Figure 5: The application runner itself. I instantiate the TimeStampingLogger and the UpperLogger. Note that I do not instantiate the StdLogger because that already is instantiated because it is an object. I chain them together and produce the needed result.

Daniel Hinojosa

About Daniel Hinojosa

Daniel is a programmer, consultant, instructor, speaker, and recent author. With over 20 years of experience, he does work for private, educational, and government institutions. He is also currently a speaker for No Fluff Just Stuff tour. Daniel loves JVM languages like Java, Groovy, and Scala; but also dabbles with non JVM languages like Haskell, Ruby, Python, LISP, C, C++. He is an avid Pomodoro Technique Practitioner and makes every attempt to learn a new programming language every year. For downtime, he enjoys reading, swimming, Legos, football, and barbecuing.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »