Prettier solution to circular dependency

Discussion in 'Ruby' started by Helder Ribeiro, Jun 11, 2007.

  1. This is an aesthetic problem: I have a method in a module, and I want
    to include it in three different classes (they're all in the same
    file). This method refers itself to one of those classes. So i have to
    include the module into the classes, but for the method to be parsed,
    one of those classes needs to be already defined.

    module ElementFactory
    def element_factory(what)
    (...)
    Element.new
    end
    end

    class Element
    include ElementFactory
    (...)
    end

    class Document
    include ElementFactory
    (...)
    end

    class ElementCollections
    include ElementFactory
    (...)
    end


    So for this class Element there's a circular dependency and, as ruby
    does single-pass parsing, this can't work. What I've done so far is to
    replace the explicit reference to Element in the module instance
    method by a call to Object.const_get:)Element) so that when it's
    called, Element will already be defined. But this looks a bit ugly.
    isn't there a better way to solve this?

    The circular dependency hints at not having the module at all and just
    putting the instance method element_factory() into Element itself but
    there are those other two classes that include it, so I can't do that.

    Thanks a lot for any ideas! :)


    Cheers,

    Helder


    --
    http://obvio171.wordpress.com

    "Then there were the lonely few of us
    who moved back and forth on the strip,
    eating rows of beads here and there,
    pretending we were Turing machines."
    -- xkcd : http://xkcd.com/c205.html
    Helder Ribeiro, Jun 11, 2007
    #1
    1. Advertising

  2. Helder Ribeiro wrote:
    > This is an aesthetic problem: I have a method in a module, and I want
    > to include it in three different classes (they're all in the same
    > file). This method refers itself to one of those classes. So i have to
    > include the module into the classes, but for the method to be parsed,
    > one of those classes needs to be already defined.
    >


    You can define the Element class at the top, and then reopen it later to
    include the module and define your methods. Just make sure that if
    Element is a subclass of something, the _first_ "class Element" must
    have the " < SuperClass". But in this case all you have to do is add
    these lines:

    class Element
    end

    > module ElementFactory
    > def element_factory(what)
    > (...)
    > Element.new
    > end
    > end
    >
    > class Element
    > include ElementFactory
    > (...)
    > end
    >
    > class Document
    > include ElementFactory
    > (...)
    > end
    >
    > class ElementCollections
    > include ElementFactory
    > (...)
    > end
    >
    >
    > So for this class Element there's a circular dependency and, as ruby
    > does single-pass parsing, this can't work. What I've done so far is to
    > replace the explicit reference to Element in the module instance
    > method by a call to Object.const_get:)Element) so that when it's
    > called, Element will already be defined. But this looks a bit ugly.
    > isn't there a better way to solve this?
    >
    > The circular dependency hints at not having the module at all and just
    > putting the instance method element_factory() into Element itself but
    > there are those other two classes that include it, so I can't do that.
    >
    > Thanks a lot for any ideas! :)
    >
    >
    > Cheers,
    >
    > Helder
    >
    >



    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Jun 11, 2007
    #2
    1. Advertising

  3. Your little snippet worked just fine for me.... ruby lets you use a
    class before it has been defined. The constant lookup is done at
    runtime.
    Caleb Clausen, Jun 11, 2007
    #3
  4. Helder Ribeiro wrote:
    > This is an aesthetic problem: I have a method in a module, and I want
    > to include it in three different classes (they're all in the same
    > file). This method refers itself to one of those classes. So i have to
    > include the module into the classes, but for the method to be parsed,
    > one of those classes needs to be already defined.


    You're still thinking in terms of static typing. In this case you
    *don't* have a circular dependency. You can load ElementFactory first
    and it doesn't matter if Element is not yet defined, as long as you
    don't invoke the method 'element_factory'

    Daniel
    Daniel DeLorme, Jun 11, 2007
    #4
  5. Helder Ribeiro

    Bas van Gils Guest

    On Mon, Jun 11, 2007 at 05:20:25PM +0900, Robert Klemme wrote:
    > Having said that I'd still consider the cyclic dependency (on the
    > logical level) bad design. We do not know more about those classes but
    > my impression is that this can probably improved upon.


    I'm new to the mailinglist .. so I'm not sure if this has been mentioned or
    not. There's a cool site which discusses the implementation of (the gang of
    four) patterns in Ruby. In this case you may want to check out

    http://www.rubypatterns.org/doku.php/gang_of_four_patterns:factory_method

    Hope this helps

    Bas

    --
    Bas van Gils <>, http://www.van-gils.org
    [[[ Thank you for not distributing my E-mail address ]]]

    Quod est inferius est sicut quod est superius, et quod est superius est sicut
    quod est inferius, ad perpetranda miracula rei unius.
    Bas van Gils, Jun 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    736
    Jonathan Mcdougall
    Jan 3, 2005
  2. Jim Langston
    Replies:
    3
    Views:
    395
    Jeff F
    Oct 5, 2007
  3. Rob Sanheim
    Replies:
    2
    Views:
    93
    Rob Sanheim
    Jul 26, 2006
  4. Christophe Mckeon

    prettier documentation

    Christophe Mckeon, Nov 17, 2007, in forum: Ruby
    Replies:
    3
    Views:
    75
    Christophe Mckeon
    Nov 19, 2007
  5. Pito Salas

    Help with making this prettier?

    Pito Salas, Jul 16, 2009, in forum: Ruby
    Replies:
    2
    Views:
    80
    Pito Salas
    Jul 16, 2009
Loading...

Share This Page