Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: ?Re: RFC: Class::DispatchToAll

by domm (Chaplain)
on Jul 11, 2002 at 08:19 UTC ( [id://180974]=note: print w/replies, xml ) Need Help??


in reply to •Re: RFC: Class::DispatchToAll
in thread RFC: Class::DispatchToAll

My Problem: I've got several classes, looking a little bit like this:
                     App
                      |
                  App::Basic
                 /         \
App::Basic::Special1     App::Basic::Special2 
     |                        |
     |                        |
     |            My::App     |
     |           /       \    |
My::App::Special1         My::App::Special2
Note that My::App doesn't inherit from App. And note that there are My::OtherApp, Your::App too, looking like My::App and inheriting from App::*

Each of this Classes contains some config values as Class Data.

I want to be able to fetch all those config values, merge them and do something with the result.

E.g.: Say, each class contains an array called @order and a hash called %lables.
@order contains some fields to be displayed
%lables contains the name of those fields as keys and a "human readable" name as values.

@order=(field1,field2,field2,..); %lable={ field1=>"Name of Field 1", field2=>"Name of Field 2", .. );

Each Class now defines some of those values. Some Classes append new values to @order and %lables, some overwrite data in %lables.

There are some basic fields (used everywhere in the app) defined in App::Basic. Each App::Basic::Special1/2 adds some fields unique to this special case.

Now, My::App changes some field lables (e.g. use another language). My::App::Special1 may add even more fields that are only needed in My::App (there might be a My::OtherApp::Special1 that inherits from App::Basic::Special1 and doesn't use this fields)

Then I call dispatch_to_all('get_fields') which fetches all those @field arrays, massage the returned array of all return values to fit my needs, and voila: I get the array of fields used by this Class, without having to specify the "Basic" fields in all Subclasses (which would result in a lot of typing and and even bigger lot of problems should I need to change a App::Basic config value)

This was/is my problem, and that's why I wrote Class::DispatchToAll, which seems the best way to solve this problem to me. But if you now another way, please let me know...

-- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

Replies are listed 'Best First'.
•Re: Re: ?Re: RFC: Class::DispatchToAll
by merlyn (Sage) on Jul 11, 2002 at 15:31 UTC
    Then I call dispatch_to_all('get_fields')
    This is where I lost you. If a class has a get_fields, I'd expect it to return all appropriate fields. If it has overridden a base class method of the same name, then there's a reason for that, so it should either call all the immediate base class methods and derive an appropriate response from that, or simply ignore those and riffle through it's own data to produce the result.

    Perhaps you don't get why "extending" and "overriding" are useful. You are breaking that model. At that point, don't call it OO programming any more. You are merely aggregating through hierarchies, turning "IS A" into "HAS A" relationships. This will scare every single maintenance programmer that has to look at your code. Please don't do that. All the studies show that most of corporate software money is spent on maintaining, not creating, the software. You have just spent a whole lot more money than you even imagine.

    -- Randal L. Schwartz, Perl hacker

      If a class has a get_fields, I'd expect it to return all appropriate fields.

      I do not want to write 100 accessor methods in each and every class, telling all those methods where to look for the data.

      Instead, I have one general accessor (get_config) in App. Through normal Perl inheritance, every class can access it.

      So, how can this one general method know which values to return? Is there a "standard" way to do this? Or is my architecture completly screwd?

      Perhaps you don't get why "extending" and "overriding" are useful.

      I know Perl OO quite well. Overriding is completly clear to me, but what do you mean by "extending"? Something like

      sub method { my $self=shift; my $return=$self->SUPER::method; # do something return $return }

      You are breaking that model.

      I know. I break it by intention, because it doesn't fit my needs. And as TheDamian is doing something similar (NEXT.pm), it seems that I am not the only one with that need...

      At that point, don't call it OO programming any more. You are merely aggregating through hierarchies, turning "IS A" into "HAS A" relationships.

      I am a so-called "accidental programmer" (i.e. I didn't learn programming at university), and while I am quite aware of OO-methodology, I cannot tell whether this is still OO or not.

      This will scare every single maintenance programmer that has to look at your code. Please don't do that

      Currently, I am maintaining an old version of this App (written by me, so it's my fault...). In the old version, all those fields are specied in there own class. I can tell you from hourlong search-and-replace-sessions that it this is quite a pain to maintain, as I have to wade to a lot of config files and change/add some values, if I change something in the whole App.

      It would seem much clearer to me if each class only adds its specif stuff (or changes already defines stuff). This is what I hope to accomplish with Class::DispatchToAll.

      -- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://180974]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found