|
visitable |
A real world example, taken from the gtk module: a Visitable
class, providing a framework for a generalized version of the
visitor pattern. For information on the visitor pattern, please
refer to:
Design Patterns - Elements of Reusable Object-Oriented Software
E.Gamma, R.Helm, R. Johnson, J. Vlissides
Addison Wesley
This example shows how to programmatically call a method given
its name: we convert the name to a symbol, then issue the
message call with basic.sendwith(), where we pass the
arguments as an array.
package Visitable;
import basic;
import string;
/*
* Visitable class
*/
public class Visitable extends basic.Object
{
method visit: visitor with: args ...
{
local methodsym;
local newargs;
local arg;
methodsym = string.makesymbol( "visit" + [[self isA] name] );
if ([visitor doesUnderstand: methodsym])
{
newargs = #[ self ];
for (arg in args)
newargs[basic.length(newargs)] = arg;
return basic.sendwith( visitor, methodsym, newargs );
}
return @nil;
}
method visit: visitor
{
return [self visit: visitor with: @nil];
}
}