Conflicting components

In the preceding example, we had a requirement for the History trait, which has an add() method. What would happen if the methods in different components have the same signatures and they clash? Let's try this:

trait Mystery {  def add(): Unit = {    System.out.println("Mystery added!")  }}

We can now use this in our Persister trait:

trait Persister[T] {  this: Database[T] with History with Mystery =>  def persist(data: T): Unit = {    System.out.println("Calling persist.")    save(data)    add()  }}

Of course, we will change all the classes that mix Persister in:

class FilePersister[T] extends Persister[T] with FileDatabase[T] with History with Mysteryclass MemoryPersister[T] extends Persister[T] with MemoryDatabase[T] with History ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.