Hello,
I have a question about Trait. Is it ok, in the design point of view, to use them to force an object to implement an interface? Like that it would be possible to check (via an #assert: for example) if a class provides the interface required. I read some things I found on the net about Traits and already used TComparable but I never created one myself and I am not sure to understand this concept entirely. Thanks in advance! Julien |
Hi Julien,
Le 22/04/2016 16:45, Julien Delplanque a écrit : > Hello, > > I have a question about Trait. > > Is it ok, in the design point of view, to use them to force an object to > implement an interface? Why not using a parent abstract class for that, with required method answering #subclassResponsability? Then from your tests in subclasses, you check self shouldnt: [instance toto] raise: SubclassResponsabilty > Like that it would be possible to check (via an #assert: for example) if > a class provides the interface required. > > I read some things I found on the net about Traits and already used > TComparable but I never created one myself and I am not sure to > understand this concept entirely. With Trait you add behavior to a class. TComparable adds behavior to your class, your instance are then comparable. > > > Thanks in advance! > > Julien > -- Dr. Geo http://drgeo.eu |
On 22/04/16 17:04, Hilaire wrote: > Hi Julien, > > > Le 22/04/2016 16:45, Julien Delplanque a écrit : >> Hello, >> >> I have a question about Trait. >> >> Is it ok, in the design point of view, to use them to force an object to >> implement an interface? > Why not using a parent abstract class for that, with required method > answering #subclassResponsability? use of inheritance? > > Then from your tests in subclasses, you check > > self shouldnt: [instance toto] raise: SubclassResponsabilty > This wont work if the object does not implement #toto at all. >> Like that it would be possible to check (via an #assert: for example) if >> a class provides the interface required. >> >> I read some things I found on the net about Traits and already used >> TComparable but I never created one myself and I am not sure to >> understand this concept entirely. > > With Trait you add behavior to a class. TComparable adds behavior to > your class, your instance are then comparable. Is it a problem? Isn't it similar to Java's interface? I see traits as a mix between Java's abstract classes and Java's interface since you can define methods and let some of them abstract but you can not define instance variables. But maybe I am wrong, that is why I ask :)... Julien |
In reply to this post by HilaireFernandes
On 22-04-16 17:04, Hilaire wrote:
> Hi Julien, > > > Le 22/04/2016 16:45, Julien Delplanque a écrit : >> Hello, >> >> I have a question about Trait. >> >> Is it ok, in the design point of view, to use them to force an object to >> implement an interface? > Why not using a parent abstract class for that, with required method > answering #subclassResponsability? > > Then from your tests in subclasses, you check > > self shouldnt: [instance toto] raise: SubclassResponsabilty > That limits one to one interface. Why not use a test testTheConformingImplementation self assert: ((AConformingImplementation selectors) asOrderedCollection addAll: (AnInterfaceClass selectors)) asSet = (AConformingImplementation selectors) asSet |
Free forum by Nabble | Edit this page |