Seeing all these good words about traits I still cannot understand, what is it all about. I see many people say traits solve Composition vs. Inheritance problem, but again, I don't see the point... Can somebody explain me on the next example?
Let's say we have trait Printing { def print () { // connect to printer // upload document to printer // ... } }
trait DatabasePersistable { def save () { // connect to database // execute SQL query // ... } }
class Document extends DatabasePersistable with Printing { def open() {...} def Pages getPages() {...} ... }
And now I want to write a true unit test for the Document class (you know, without those heavy dependencies on the outside environment and resources). I need to cut off database and printing technologies dependencies for this purpose to test solely methods of the Document class. In Java I would use composition and just inject fake, mock, stub or something else instead. How do I do this in this Scala example?
P.S. Please, do not see this as critics of Java, Scala or inheritance, I just want to understand how this works.