Hello winscript-talks,
http://www.artima.com/intv/tuesday.html
...
Bill Venners: In Ruby, I can add methods and variables to objects at runtime. I have certainly
added methods and variables to classes, which become objects at runtime, in other languages.
But in Java, for example, once a class is loaded or an object is instantiated, its interface
stays the same. Allowing the interface to change at runtime seems a bit scary to me. I'm
curious how I would use that feature in Ruby. What's the benefit of being able to add
methods at runtime?
Yukihiro Matsumoto: First of all, you don't have to use that feature. The most useful application
of dynamic features, such as adding methods to objects, is meta-programming. Such features
allow you to create a library that adapts to the environment, but they are not for
casual uses.
Bill Venners: What's an example of a library that adapts to the environment?
Yukihiro Matsumoto: One example is a proxy. Instead of designing individual proxy classes for
each particular class, in Ruby you can create an all purpose proxy that can wrap any object. The
proxy can probe the object inside of it and just morph into the proxy for that object. The proxy
can add methods to itself so it has the same interface as the wrapped object, and each of
those methods can delegate to the corresponding method in the wrapped object. So an all-purpose
proxy, which can be used to wrap any object, is an example of how a library class can adapt to
the environment.
Adding methods to objects can also be used in Ruby in situations where Java programmers use
inner classes. For example, if you need to pass a listener object to some method, in Java
you often instantiate an anonymous inner class that defines the listener methods and pass
it. In Ruby, you can just create a plain object≈an instance of class Object≈add the needed
listener methods to it dynamically, and pass it.
...
Кстати, я помню такой же довод от В. Метелицы, он говорил, что в Java
версии TopLink приходится для каждого класса генерировать несколько Proxy
--
Best regards,
Max mailto:be...@ma...
http://belugin.newmail.ru ICQ:9406811
|