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
trait PipelineComponent { | |
def execute: Unit | |
} | |
trait Generator extends PipelineComponent { | |
override def execute: Unit = print("Generator executing\n") | |
} | |
trait Transformer extends PipelineComponent { | |
override def execute: Unit = print("Transformer executing\n") | |
} | |
trait Serializer extends PipelineComponent { | |
override def execute: Unit = print("Serializer executing\n") | |
} | |
abstract class AbstractPipeline(pipelineComponents: List[PipelineComponent]) | |
case class EmptyPipeline extends AbstractPipeline(List()) { | |
def add(generator: Generator): NonEmptyPipeline = new NonEmptyPipeline(List(generator)) | |
} | |
case class NonEmptyPipeline(pipelineComponents: List[PipelineComponent]) extends AbstractPipeline(pipelineComponents) { | |
def add(transformer: Transformer): NonEmptyPipeline = new NonEmptyPipeline(pipelineComponents :+ transformer) | |
def add(serializer: Serializer): FinishedPipeline = new FinishedPipeline(pipelineComponents :+ serializer) | |
} | |
case class FinishedPipeline(pipelineComponents: List[PipelineComponent]) extends AbstractPipeline(pipelineComponents) { | |
def execute(): Unit = pipelineComponents.foreach(_.execute) | |
} | |
/** | |
object PipelineApp extends App { | |
val generator = new Object with Generator | |
val transformer1 = new Object with Transformer | |
val transformer2 = new Object with Transformer | |
val serializer = new Object with Serializer | |
new EmptyPipeline().add(generator).add(transformer1).add(transformer2).add(serializer).execute | |
} | |
this will print: | |
Generator executing | |
Transformer executing | |
Transformer executing | |
Serializer executing | |
**/ |
No comments:
Post a Comment