Created
December 3, 2014 16:37
-
-
Save igstan/62a6732db7c6f6e610a4 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
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