[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Help with facade pattern

djberg96

5/18/2005 3:54:00 AM

Hi all,

Well, I *think* it's the facade pattern. Anyway, I'm trying to do
something like this:

class Foo < String
def method_a
Bar.method_a(self)
end
def method_b
Bar.method_b(self)
end
end

This is easy enough to do by hand for a few methods, but what if I have
many methods from Bar I want to use?

I thought maybe there was a trick with Forwardable I could use, but I
don't see how. Any ideas on how to shortcut this?

Regards,

Dan

2 Answers

ES

5/18/2005 4:15:00 AM

0

Daniel Berger wrote:
> Hi all,
>
> Well, I *think* it's the facade pattern. Anyway, I'm trying to do
> something like this:
>
> class Foo < String
> def method_a
> Bar.method_a(self)
> end
> def method_b
> Bar.method_b(self)
> end
> end
>
> This is easy enough to do by hand for a few methods, but what if I have
> many methods from Bar I want to use?
>
> I thought maybe there was a trick with Forwardable I could use, but I
> don't see how. Any ideas on how to shortcut this?

You left too quick :) You are looking at something like this:

class Class
def forward(mod, *methods)
methods.each do |method|
# Substitute mod with const_get(mod) if you want to use :ModName
define_method(method) {|| mod.send(method, self)}
end
end
end

class A
def A.foo(x)
puts "A.foo: #{x}"
end
end

class B
forward A, :foo
end

B.new.foo # => A.foo

> Regards,
>
> Dan

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }


Daniel Berger

5/18/2005 2:21:00 PM

0

ES wrote:
> Daniel Berger wrote:
> > Hi all,
> >
> > Well, I *think* it's the facade pattern. Anyway, I'm trying to do
> > something like this:
> >
> > class Foo < String
> > def method_a
> > Bar.method_a(self)
> > end
> > def method_b
> > Bar.method_b(self)
> > end
> > end
> >
> > This is easy enough to do by hand for a few methods, but what if I
have
> > many methods from Bar I want to use?
> >
> > I thought maybe there was a trick with Forwardable I could use, but
I
> > don't see how. Any ideas on how to shortcut this?
>
> You left too quick :) You are looking at something like this:
>
> class Class
> def forward(mod, *methods)
> methods.each do |method|
> # Substitute mod with const_get(mod) if you want to use
:ModName
> define_method(method) {|| mod.send(method, self)}
> end
> end
> end
>
> class A
> def A.foo(x)
> puts "A.foo: #{x}"
> end
> end
>
> class B
> forward A, :foo
> end
>
> B.new.foo # => A.foo
>
> > Regards,
> >
> > Dan
>
> E
>
> --
> template<typename duck>
> void quack(duck& d) { d.quack(); }


Thank you - that will work beautfully. :)

Regards,

Dan