[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: OO Challenge

Weirich, James

9/12/2003 5:20:00 PM

From: Robert Klemme [mailto:bob.news@gmx.net]
> "Weirich, James" <James.Weirich@FMR.COM> schrieb im Newsbeitrag
> > [...] last bits of our programs are identical
> > except for minor spelling differences.
>
> Probably there's not too much room for variations with this
> small example.

Add the abstractions were fairly well identified by the OP as well. What's
significant (IMHO) is that once the abstractions are set, the code using
those abstractions is pretty straight-forward.

> Rereading the original posting I think the author of these lines has not
> yet fully understand OO:

I think you are talking about the Icon programmer here. What I find
interesting is that he denigrates OO, but provides a solution that depends
on a polymorphic function (although he implements the polymorphism by hand).

> In fact, any of the ruby solutions looks cleaner to me
> than the VB examples. My 2 cent...

Actually, I think the first example is Icon, not VB. But I agree, the Ruby
stuff does look cleaner to me.

> Great short implementation! I always like to see how small
> programs can get in Ruby. Unfortunately that reminds me that
> I sometimes do seem to make things unnecessary complicated...
> *sigh*

Actually, I think my example is a little over simplified for real life work.
Using a proc for the tax group is cute, but doesn't reflect the abstraction
well (you have to use "call" instead of something more descriptive). A
slightly longer version would be ...

class SimpleTaxClass
def initialize(&block)
@calculations = block
end
def calculate_tax(tp)
@calculations.call(tp)
end
end

Soldier = SimpleTaxClass.new { |tp| tp.salary / 10.0 }
Professor = SimpleTaxClass.new { |tp| tp.salary / 15.0 + 100 }
etc...

The problem gets interesting when you start throwing changes at it. For
example, what if there was another class of taxpayer that selected the
minimum tax charge. We could code this up as ...

class Politician < TaxPayer
def tax
@tax_classes.collect { |tc| tc.calculate_tax(self) }.min
end
end

Then we can have ...

taxpayers = [
TaxPayer.new(3000, [Soldier]),
Politician.new(4000, [Soldier, Professor])
]

Now think about what it would take to have the politician behavior
determined at runtime. Fun stuff.

--
-- Jim Weirich / Compuware
-- FWP Capture Services
-- Phone: 859-386-8855