Lähettäjä: Edgardo Hames <ehames@gmail.com>
Aihe: "Duck Typing" or "No need for abstract classes"Hi, you all.
I'm working on a new small project where several network protocols are
to be supported. I had an idea about how to implement it and it
resembled something like this.class Protocol
abstract :login, :logout, :send, :receive
endclass MyProtocol < Protocol
def login
# does something useful
end
... # so on
endThen my network client would just call the methods of a Protocol
object without caring about the actual classes that implement it (yes,
you're right, I come from a static typing background). But then I read
something (I don't remember where ) which said that agile languages
don't need to implement so many patterns and I think I saw the light!I don't need a Protocol class, the network client should just call the
methods of the protocol and duck typing should do all the magic. Am I
right? Am I coming a little closer to walking the Ruby Way?Please, post a positive reply and you just may save me a couple of
therapy sessions
Yep, you're right. It doesn't matter what the type of an object you
have as long as it responds to the method you're trying to invoke.
If it walks like a...
Kind Regards,
Ed
E
P.S. Object#respond_to? is a good method to keep in mind.