scala - How to determine to use trait to 'with' or class to 'inject'? -
i'm puzzled choose trait
or class
when writing scala code.
at first, have controller with
several traits:
class mycontroller extends controller transactionsupport jsonconvertersupport loggersupport
in these traits, defined methods , fields can used in mycontroller
directly.
but friend says: when extends
or with
trait, should be a
trait.
look @ mycontroller
, controller
, isn't transactionsupport
, not jsonconvertersupport
, not loggersupport
, should not with
them.
so code becomes:
class mycontroller(transupport: transactionsupport, jsonconverter: jsonconvertersupport, loggersupport: loggersupport) extends controller
but don't feel code, seems strange.
i see trait
s used heavily in scala code, when should use or use classes inject?
i'll refer interfaces should adjectives. though traits may play part of class (and, therefore, nouns , respect "is-a" relationship), when used mixins they'll tend play part of interfaces.
as "adjective", trait add qualifying property whatever extending. example, may comparable
or serializable
.
it can bit hard find adjective fit -- adjective use loggersupport
? -- don't feel overly constrained that. aware wrong thing of traits "is-a" relationship.
i try avoid using traits replace "has-a" relationships, though.
Comments
Post a Comment