Skip to content

Instantly share code, notes, and snippets.

@igstan
Created December 3, 2014 16:37
Show Gist options
  • Save igstan/62a6732db7c6f6e610a4 to your computer and use it in GitHub Desktop.
Save igstan/62a6732db7c6f6e610a4 to your computer and use it in GitHub Desktop.
sealed trait Content
case class One(value: String) extends Content
case class Two(value: Int) extends Content
sealed trait AbstractCommand
case class IndexCommand(c: String) extends AbstractCommand
case class UpdateCommand(c: Int) extends AbstractCommand
trait Indexer {
def index(content: Content): Unit
def index(contents: Iterable[Content]): Unit
}
trait Client {
def execute(command: IndexCommand): Unit
def execute(command: UpdateCommand): Unit
}
class IndexerImpl extends Indexer {
val client = new Client {
override def execute(command: IndexCommand): Unit = ???
override def execute(command: UpdateCommand): Unit = ???
}
implicit class ClientExtra(client: Client) {
def executeCommand(command: AbstractCommand): Unit = {
command match {
case c: IndexCommand => client.execute(c)
case c: UpdateCommand => client.execute(c)
}
}
}
override def index(content: Content): Unit = client.executeCommand(createCommand(content))
override def index(contents: Iterable[Content]): Unit = {
contents.foreach(c => client.executeCommand(createCommand(c)))
}
private def createCommand(content: Content): AbstractCommand = {
content match {
case One(c) => new IndexCommand(c)
case Two(c) => new UpdateCommand(c)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment