From: "Mauricio Fernández" Date: 2003-06-19T03:28:02+09:00 Subject: Re: switching multiple interfaces of an object On Thu, Jun 19, 2003 at 12:35:12AM +0900, ahoward wrote: > On Wed, 18 Jun 2003, Mauricio [iso-8859-1] Fern�ndez wrote: > > > Is this useful at all? > > very. > > > I thought of this as I was writing on the Adaptor pattern. I consider > > the above to be a "generalized" (dynamic) adaptor. > > looks very cool. i've done something similar (i think) using delegation: > [some 51 lines of code] > care to comment on the relative merits/drawbacks of these approaches? i really > do think something standard like this would be extremely useful. Ummm, your code is *way cleaner and simpler* :-) My code basically involves aliasing methods as needed to get the right ones to have the required name. Cons: (w.r.t. delegation) * threading issues: the current interface used for one object must be the same in all threads (or non-interfering interfaces) * more complicated * uses 1 class instance variable (bad in the sense that if you mess with that it breaks :) Pros: (ditto) * in your code you have to create internal classes, whereas I am only adding methods and saying "methods foo, bar and baz as defined right now correspond to interface Babar". So I think although the implementation I did is more complicated, its use is actually easier. * this means your classes don't normally share state unless you do explicitly pass it around (which you do in your code), so it seems more difficult to do this "extend and define interface" thing and don't have direct access to instance vars * it can be made to work with singleton methods, so they get registered as part of the interface. * therefore w/ your code interfaces and their implementations have to be known beforehand, whereas with mine it's easier to add them on the fly (Object#extend). You can find the code at http://www.rubygarden.org/ruby?AdaptorPattern/Generalized If there's interest in a such a thing I have a couple ideas which should work much better. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com "You, sir, are nothing but a pathetically lame salesdroid! I fart in your general direction!" -- Randseed on #Linux