|XP is just a number
Re: Inherit from a Role (or something like that)by John M. Dlugosz (Monsignor)
|on May 20, 2011 at 20:07 UTC
Have you read the original "traits" paper?
but now how do I write my other classes against this role?Other code should be written to use only the API provided by the RemoteConnection role, without caring what class it is otherwise. Any type checking should use the role name.
but then I can't easily inherit from both of them in Server and Client.That sounds like a different problem. Maybe you didn't explain it well? Are you saying that Server and Client each want a different derived class of the Connection, but the Connection may already be different concrete classes?
You need to aggregate rather than further derive. Have the Connection class that you will be deriving from contain a SSH or Socket back-end instance, rather than being derived from it.
In fact, you might not need inheritance in the other place, either. Look at using collaborating instances instead. You can use delegation to make the result just as slick as inheritance rather than having to forward everything all over the place. Delegation can easily behave like inheritance from a run-time specified instance.
See this node for more recent discussion on delegation.