Skip to content

Instantly share code, notes, and snippets.

@yutaono
Created April 28, 2015 07:41
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 yutaono/7c8297ce08c63878973b to your computer and use it in GitHub Desktop.
Save yutaono/7c8297ce08c63878973b to your computer and use it in GitHub Desktop.
stackable trait pattern
// stackable trait pattern
// http://www.artima.com/scalazine/articles/stackable_trait_pattern.html
abstract class IntQueue {
def get: Int
def put(x: Int)
}
import scala.collection.mutable.ArrayBuffer
class BasicIntQueue extends IntQueue {
private val buf = new ArrayBuffer[Int]
def get = buf.remove(0)
def put(x: Int) = buf += x
}
trait Doubling extends IntQueue {
abstract override def put(x: Int) = super.put(x * 2)
}
trait Incrementing extends IntQueue {
abstract override def put(x: Int) = super.put(x + 1)
}
trait Filtering extends IntQueue {
abstract override def put(x: Int) = if (x >= 0) super.put(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment