"Mathieu Bouchard" <matju / sympatico.ca> schrieb im Newsbeitrag
news:Pine.LNX.4.21.0501261052280.16754-100000 / mondrian.artengine.ca...
>
> On Tue, 25 Jan 2005, Robert Klemme wrote:
>
> > > Actually, Perl has it better, because you can change the class of an
> > > object, and you can change the list of superclasses of any class.
> > And if a copy is sufficient you can do this even in pure Ruby:
>
> No, a copy is _not_ sufficient.
>
> > Do you have a link or explanation that demonstrates the merits of the
> > capability to change an instance's class at runtime?  Currently I
don't
> > have a clue in which situations I would want this.  Thx!
>
> Just like almost any other programming concept invented to improve
> structured programming, it's about replacing if/case expressions by
tables
> functionpointers such that it creates a "separation of concerns". (i
mean
> all the various forms of polymorphisms)
>
> Now, for that feature in particular, think of a File which may be open
or
> closed, and you have to write at the start of each damn method:
>
>   if not @file then raise "file not open" end
>
> well, this could be solved by changing the set of all methods of that
> class.
>
> In general an object that has a few major cases of behaviour like that,
> can benefit from switching methodtables.

So basically you remove the wrapping instance from strategy pattern.
There will be different opinions about whether the distinction is more
approrpiately modeled by an object that changes its class from ClosedFile
to OpenFile or by an object with class File which changes its state from
"open" to "closed".  I for my part prefer the second variant but I can see
the benefit of the other approach.

> In Ruby, my way to solve this problem is instead to use one object
> representing a File that may be open or not, and one object that
> represents a File guaranteed to be open, and then use message-forwarding
> using Object#method_missing.

Some more reading on the patterns discussed:

http://c2.com/cgi/wiki?StatePattern
http://c2.com/cgi/wiki?StrategyPattern

Kind regards

    robert