[Israel.pm] An OOP Pattern Discovered by Accident

Shlomi Fish shlomif at gmail.com
Sun Jan 28 06:23:51 PST 2007


On 1/28/07, Oded Arbel <oded-linux at typo.co.il> wrote:
> On Sun, 2007-01-28 at 14:41 +0200, Shlomi Fish wrote:
> > Hi Oded! And All.
> >
> > On 1/28/07, Oded Arbel <oded-linux at typo.co.il> wrote:
> > > On Sat, 2007-01-27 at 23:04 +0200, Shlomi Fish wrote:
> > > > > Eh. That's the one thing I'm missing when comparing perl to Java :
> > > > > automated refactoring.
> > > >
> > > > Well, there's a difference between automated refactoring and automated code
> > > > generation, which is what you've mentioned before.
> > >
> > > Nitpicking.
> > >
> >
> > Actually there is a difference. In order to automatically refactor
> > code one must analyse the existing code
>
> Not much - refactoring is a type of code generation (albeit it also
> changes existing code in addition to generating new code - so you can
> say that automatic code generation is a subset of automatic
> refactoring).

Well, automatic refactoring cannot be a superset of automatic code
generation,  because some code generation extends the functionality of
the code, while refactoring always retains the same functionality,
only improves the quality of the code.

> Automatic code generation is Eclipse does require analysis
> as the following example will show.
>

OK.

> > > while I'm
> > > looking for something more.. how shall I say it... ehmm... automatic.
> > > Hey! that's the word I used before, I appear to be repeating myself :-(
> >
> > It would be helpful if you explained how Eclipse's code generation is
> > more "automatic". Do you give it a function as a template and then can
> > generate other functions based on it.
>
> For example, for the delegate pattern discussed previously, I choose
> "generated delegate methods" from a menu, and eclipse pulls up an
> interactive dialog with a list of possible contained objects to which
> I'd wont to delegate. I then select the objects I want to delegate to
> (with the option of fine tuning by selecting only specific methods that
> will be delegated) and hit OK. Eclipse then adds delegate methods
> automatically in the form:
> <method signature> {
>         return m_delegate.<method signature>
> }
>

I see.

> The template can be modified as all other templates in Eclipse, but I
> never had to bother with it - its simple enough that it works w/o
> twiddling. Other code generation routines (implement interfaces for
> example) work similarly or w/o the need for additional user
> interaction.
> Its automatic in the sense that I don't have to write a single line of
> code in order to generate a lot of code.

OK. Well, it's still requires you to perform some commands using the
mouse, etc, so it's not fully automatic, especially if you are
selecting a list of methods. This is similar to writing an editor
macro or the code I demonstrated, with the added benefit that you can
select from the list of existing methods, and class members.

>
> > From a purist POV, they would dislike the idea of having such
> > duplicate code even if the IDE generates and manages it.
>
> Agreed, but then there will always be purists and we don't have to take
> their pov into consideration, knowing that its based only on
> conservatism. see edlin vs. vi.

Edlin vs. vi? What do you mean? Didn't you mean Emacs vs. vi?

>
> > My perl example can be encapsulated in a module and
> > then one can say something along the lines of:
> >
> > use base 'Class::MakeDelegates';
> >
> > __PACKAGE__->mk_delegate($delegate_to_property, [qw(method1
> > second_method write_line)]);
> >
> > To generate all the delegated methods.
>
> Then it would be great if you do us the service of submitting it to
> CPAN.
>

Well, a quick search on CPAN yielded the following module:

http://search.cpan.org/dist/Class-Delegate/

It seems to take a different approach than what I demonstrated -
delegations are done on an instance-by-instance basis, and it defaults
to delegating all methods. It also uses AUTOLOAD.

Also, it seems that Class::Methodist (
http://search.cpan.org/dist/Class-Methodist/ ) and  Class::MakeMethods
( http://search.cpan.org/dist/Class-MakeMethods/ , albeit the delegate
featuer there is poorly documented) also support delegation and this
time with an optional of declaring methods like I did. I don't know
about Class::MethodMaker or Moose.

Do you still think there's a point to my own CPAN distribution doing that?

Regards,

          Shlomi Fish

------------------------------------------
Shlomi Fish http://www.shlomifish.org/

If his programming is anything like his philosophising, he
would find 10 imaginary bugs in the "Hello World" program.



More information about the Perl mailing list