Dealing with a Circular Dependency











up vote
9
down vote

favorite
3












I wonder if someone can advise on any good ways to break a circular dependency between 2 classes in Java.FindBugs proposes the use of interfaces so i wonder if someone has any good experience with this type of problem.










share|improve this question




















  • 1




    What is wrong with circular references in the first place? Why would you want to break them? Maybe you should point this out in your question.
    – Mecki
    Mar 30 '11 at 17:59






  • 1




    well, i dont know ... supposing is an anti-pattern i want to avoid ! and for educational purposes, to check different design possibilities.!
    – tropicana
    Mar 30 '11 at 18:03










  • Circular refs are an anti-pattern? Says who? The Java GC has no problem with them for instances (only a GC that use pure reference counting would have such an issue) and the compiler has no issue with them at compile time, other than that you must compile both classes at the same time, so the compiler is able to resolve the dependencies. BTW, you missed an important tag: java. I'll fix the tags for you.
    – Mecki
    Mar 31 '11 at 11:48








  • 1




    @Mecki: Circular class dependencies are a different thing than cycles in the object graph at runtime. I'd agree that circular class dependencies should be avoided where possible, and use of interfaces to break the dependency is a good approach.
    – andersoj
    Mar 31 '11 at 11:51






  • 1




    @Chris: If you give us an example of your circular dependent classes, we can give better advice.
    – Paŭlo Ebermann
    Mar 31 '11 at 11:56















up vote
9
down vote

favorite
3












I wonder if someone can advise on any good ways to break a circular dependency between 2 classes in Java.FindBugs proposes the use of interfaces so i wonder if someone has any good experience with this type of problem.










share|improve this question




















  • 1




    What is wrong with circular references in the first place? Why would you want to break them? Maybe you should point this out in your question.
    – Mecki
    Mar 30 '11 at 17:59






  • 1




    well, i dont know ... supposing is an anti-pattern i want to avoid ! and for educational purposes, to check different design possibilities.!
    – tropicana
    Mar 30 '11 at 18:03










  • Circular refs are an anti-pattern? Says who? The Java GC has no problem with them for instances (only a GC that use pure reference counting would have such an issue) and the compiler has no issue with them at compile time, other than that you must compile both classes at the same time, so the compiler is able to resolve the dependencies. BTW, you missed an important tag: java. I'll fix the tags for you.
    – Mecki
    Mar 31 '11 at 11:48








  • 1




    @Mecki: Circular class dependencies are a different thing than cycles in the object graph at runtime. I'd agree that circular class dependencies should be avoided where possible, and use of interfaces to break the dependency is a good approach.
    – andersoj
    Mar 31 '11 at 11:51






  • 1




    @Chris: If you give us an example of your circular dependent classes, we can give better advice.
    – Paŭlo Ebermann
    Mar 31 '11 at 11:56













up vote
9
down vote

favorite
3









up vote
9
down vote

favorite
3






3





I wonder if someone can advise on any good ways to break a circular dependency between 2 classes in Java.FindBugs proposes the use of interfaces so i wonder if someone has any good experience with this type of problem.










share|improve this question















I wonder if someone can advise on any good ways to break a circular dependency between 2 classes in Java.FindBugs proposes the use of interfaces so i wonder if someone has any good experience with this type of problem.







java circular-dependency






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 9:08









syntagma

12.4k1248104




12.4k1248104










asked Mar 30 '11 at 17:52









tropicana

70911420




70911420








  • 1




    What is wrong with circular references in the first place? Why would you want to break them? Maybe you should point this out in your question.
    – Mecki
    Mar 30 '11 at 17:59






  • 1




    well, i dont know ... supposing is an anti-pattern i want to avoid ! and for educational purposes, to check different design possibilities.!
    – tropicana
    Mar 30 '11 at 18:03










  • Circular refs are an anti-pattern? Says who? The Java GC has no problem with them for instances (only a GC that use pure reference counting would have such an issue) and the compiler has no issue with them at compile time, other than that you must compile both classes at the same time, so the compiler is able to resolve the dependencies. BTW, you missed an important tag: java. I'll fix the tags for you.
    – Mecki
    Mar 31 '11 at 11:48








  • 1




    @Mecki: Circular class dependencies are a different thing than cycles in the object graph at runtime. I'd agree that circular class dependencies should be avoided where possible, and use of interfaces to break the dependency is a good approach.
    – andersoj
    Mar 31 '11 at 11:51






  • 1




    @Chris: If you give us an example of your circular dependent classes, we can give better advice.
    – Paŭlo Ebermann
    Mar 31 '11 at 11:56














  • 1




    What is wrong with circular references in the first place? Why would you want to break them? Maybe you should point this out in your question.
    – Mecki
    Mar 30 '11 at 17:59






  • 1




    well, i dont know ... supposing is an anti-pattern i want to avoid ! and for educational purposes, to check different design possibilities.!
    – tropicana
    Mar 30 '11 at 18:03










  • Circular refs are an anti-pattern? Says who? The Java GC has no problem with them for instances (only a GC that use pure reference counting would have such an issue) and the compiler has no issue with them at compile time, other than that you must compile both classes at the same time, so the compiler is able to resolve the dependencies. BTW, you missed an important tag: java. I'll fix the tags for you.
    – Mecki
    Mar 31 '11 at 11:48








  • 1




    @Mecki: Circular class dependencies are a different thing than cycles in the object graph at runtime. I'd agree that circular class dependencies should be avoided where possible, and use of interfaces to break the dependency is a good approach.
    – andersoj
    Mar 31 '11 at 11:51






  • 1




    @Chris: If you give us an example of your circular dependent classes, we can give better advice.
    – Paŭlo Ebermann
    Mar 31 '11 at 11:56








1




1




What is wrong with circular references in the first place? Why would you want to break them? Maybe you should point this out in your question.
– Mecki
Mar 30 '11 at 17:59




What is wrong with circular references in the first place? Why would you want to break them? Maybe you should point this out in your question.
– Mecki
Mar 30 '11 at 17:59




1




1




well, i dont know ... supposing is an anti-pattern i want to avoid ! and for educational purposes, to check different design possibilities.!
– tropicana
Mar 30 '11 at 18:03




well, i dont know ... supposing is an anti-pattern i want to avoid ! and for educational purposes, to check different design possibilities.!
– tropicana
Mar 30 '11 at 18:03












Circular refs are an anti-pattern? Says who? The Java GC has no problem with them for instances (only a GC that use pure reference counting would have such an issue) and the compiler has no issue with them at compile time, other than that you must compile both classes at the same time, so the compiler is able to resolve the dependencies. BTW, you missed an important tag: java. I'll fix the tags for you.
– Mecki
Mar 31 '11 at 11:48






Circular refs are an anti-pattern? Says who? The Java GC has no problem with them for instances (only a GC that use pure reference counting would have such an issue) and the compiler has no issue with them at compile time, other than that you must compile both classes at the same time, so the compiler is able to resolve the dependencies. BTW, you missed an important tag: java. I'll fix the tags for you.
– Mecki
Mar 31 '11 at 11:48






1




1




@Mecki: Circular class dependencies are a different thing than cycles in the object graph at runtime. I'd agree that circular class dependencies should be avoided where possible, and use of interfaces to break the dependency is a good approach.
– andersoj
Mar 31 '11 at 11:51




@Mecki: Circular class dependencies are a different thing than cycles in the object graph at runtime. I'd agree that circular class dependencies should be avoided where possible, and use of interfaces to break the dependency is a good approach.
– andersoj
Mar 31 '11 at 11:51




1




1




@Chris: If you give us an example of your circular dependent classes, we can give better advice.
– Paŭlo Ebermann
Mar 31 '11 at 11:56




@Chris: If you give us an example of your circular dependent classes, we can give better advice.
– Paŭlo Ebermann
Mar 31 '11 at 11:56












3 Answers
3






active

oldest

votes

















up vote
6
down vote













Circular dependencies aren't always to be avoided. I'd avoid them in the large, but keeps in small tight corners of a system. In the large, i.e if data access layer and the representation layer of J2EE app circular dependent, I'd say that is a bad thing, because it means that everything has to be compiled in one go and testing is nightmare. But, it's no problem if a list data-structure and its iterator type are circular dependend.



As Findbugs suggests use interfaces to break a circular dependency. I.e introduce a interface for at least one type of the circle and make the other classes use the interface everywhere. Do you need example code?






share|improve this answer





















  • +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
    – Mark McKenna
    Oct 5 '11 at 16:30










  • By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
    – jmg
    Oct 6 '11 at 8:21


















up vote
5
down vote













Suggest reading about the dependency inversion principle, e.g. What is the Dependency Inversion Principle and why is it important? or http://en.wikipedia.org/wiki/Dependency_inversion_principle






share|improve this answer























  • Downvoter-- why?
    – andersoj
    Mar 4 '12 at 23:38










  • Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
    – sceiler
    May 19 '16 at 6:59


















up vote
2
down vote













There's a blog post here on how Restructure101 was used to remove cyclic dependencies, "tangles", from Junit and a presentation from the Lausanne JUG on how it was used to remove tangles from Icefaces.



As for the debate on whether cyclic dependencies are bad, I suggest reading Uncle Bob's Solid Principles.



Disclaimer: I work for Headway Software the developers of Restructure101.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5490132%2fdealing-with-a-circular-dependency%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote













    Circular dependencies aren't always to be avoided. I'd avoid them in the large, but keeps in small tight corners of a system. In the large, i.e if data access layer and the representation layer of J2EE app circular dependent, I'd say that is a bad thing, because it means that everything has to be compiled in one go and testing is nightmare. But, it's no problem if a list data-structure and its iterator type are circular dependend.



    As Findbugs suggests use interfaces to break a circular dependency. I.e introduce a interface for at least one type of the circle and make the other classes use the interface everywhere. Do you need example code?






    share|improve this answer





















    • +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
      – Mark McKenna
      Oct 5 '11 at 16:30










    • By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
      – jmg
      Oct 6 '11 at 8:21















    up vote
    6
    down vote













    Circular dependencies aren't always to be avoided. I'd avoid them in the large, but keeps in small tight corners of a system. In the large, i.e if data access layer and the representation layer of J2EE app circular dependent, I'd say that is a bad thing, because it means that everything has to be compiled in one go and testing is nightmare. But, it's no problem if a list data-structure and its iterator type are circular dependend.



    As Findbugs suggests use interfaces to break a circular dependency. I.e introduce a interface for at least one type of the circle and make the other classes use the interface everywhere. Do you need example code?






    share|improve this answer





















    • +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
      – Mark McKenna
      Oct 5 '11 at 16:30










    • By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
      – jmg
      Oct 6 '11 at 8:21













    up vote
    6
    down vote










    up vote
    6
    down vote









    Circular dependencies aren't always to be avoided. I'd avoid them in the large, but keeps in small tight corners of a system. In the large, i.e if data access layer and the representation layer of J2EE app circular dependent, I'd say that is a bad thing, because it means that everything has to be compiled in one go and testing is nightmare. But, it's no problem if a list data-structure and its iterator type are circular dependend.



    As Findbugs suggests use interfaces to break a circular dependency. I.e introduce a interface for at least one type of the circle and make the other classes use the interface everywhere. Do you need example code?






    share|improve this answer












    Circular dependencies aren't always to be avoided. I'd avoid them in the large, but keeps in small tight corners of a system. In the large, i.e if data access layer and the representation layer of J2EE app circular dependent, I'd say that is a bad thing, because it means that everything has to be compiled in one go and testing is nightmare. But, it's no problem if a list data-structure and its iterator type are circular dependend.



    As Findbugs suggests use interfaces to break a circular dependency. I.e introduce a interface for at least one type of the circle and make the other classes use the interface everywhere. Do you need example code?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 3 '11 at 11:02









    jmg

    6,11611320




    6,11611320












    • +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
      – Mark McKenna
      Oct 5 '11 at 16:30










    • By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
      – jmg
      Oct 6 '11 at 8:21


















    • +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
      – Mark McKenna
      Oct 5 '11 at 16:30










    • By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
      – jmg
      Oct 6 '11 at 8:21
















    +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
    – Mark McKenna
    Oct 5 '11 at 16:30




    +1. Good design methodologies are good by virtue of having a clear benefit. In the specific circumstance of a collection and its (lazy) iterator for example, introducing a new interface simply to break the cycle is overkill. However, I would argue this: only use circular dependencies in circumstances where you can include all but one of the dependencies as inner or nested classes. In fact by using inner classes in this way you can avoid many circular dependency alerts, by using the implicit container reference that the inner class keeps.
    – Mark McKenna
    Oct 5 '11 at 16:30












    By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
    – jmg
    Oct 6 '11 at 8:21




    By using the implicit reference of inner classes you might avoid warning of tools, but your code still has circular references. But as I said, I'd advice to make the circles as small as possible - not to avoid them at all costs. And by using inner classes you do that.
    – jmg
    Oct 6 '11 at 8:21












    up vote
    5
    down vote













    Suggest reading about the dependency inversion principle, e.g. What is the Dependency Inversion Principle and why is it important? or http://en.wikipedia.org/wiki/Dependency_inversion_principle






    share|improve this answer























    • Downvoter-- why?
      – andersoj
      Mar 4 '12 at 23:38










    • Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
      – sceiler
      May 19 '16 at 6:59















    up vote
    5
    down vote













    Suggest reading about the dependency inversion principle, e.g. What is the Dependency Inversion Principle and why is it important? or http://en.wikipedia.org/wiki/Dependency_inversion_principle






    share|improve this answer























    • Downvoter-- why?
      – andersoj
      Mar 4 '12 at 23:38










    • Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
      – sceiler
      May 19 '16 at 6:59













    up vote
    5
    down vote










    up vote
    5
    down vote









    Suggest reading about the dependency inversion principle, e.g. What is the Dependency Inversion Principle and why is it important? or http://en.wikipedia.org/wiki/Dependency_inversion_principle






    share|improve this answer














    Suggest reading about the dependency inversion principle, e.g. What is the Dependency Inversion Principle and why is it important? or http://en.wikipedia.org/wiki/Dependency_inversion_principle







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 23 '17 at 12:09









    Community

    11




    11










    answered Mar 31 '11 at 11:56









    andersoj

    17.2k64971




    17.2k64971












    • Downvoter-- why?
      – andersoj
      Mar 4 '12 at 23:38










    • Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
      – sceiler
      May 19 '16 at 6:59


















    • Downvoter-- why?
      – andersoj
      Mar 4 '12 at 23:38










    • Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
      – sceiler
      May 19 '16 at 6:59
















    Downvoter-- why?
    – andersoj
    Mar 4 '12 at 23:38




    Downvoter-- why?
    – andersoj
    Mar 4 '12 at 23:38












    Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
    – sceiler
    May 19 '16 at 6:59




    Probably because of the missing context around your linked URLs. URLs are encouraged for further reading but the answer should contain some context and the essence of the URLs. Please see this: stackoverflow.com/help/how-to-answer (Provide context for links)
    – sceiler
    May 19 '16 at 6:59










    up vote
    2
    down vote













    There's a blog post here on how Restructure101 was used to remove cyclic dependencies, "tangles", from Junit and a presentation from the Lausanne JUG on how it was used to remove tangles from Icefaces.



    As for the debate on whether cyclic dependencies are bad, I suggest reading Uncle Bob's Solid Principles.



    Disclaimer: I work for Headway Software the developers of Restructure101.






    share|improve this answer



























      up vote
      2
      down vote













      There's a blog post here on how Restructure101 was used to remove cyclic dependencies, "tangles", from Junit and a presentation from the Lausanne JUG on how it was used to remove tangles from Icefaces.



      As for the debate on whether cyclic dependencies are bad, I suggest reading Uncle Bob's Solid Principles.



      Disclaimer: I work for Headway Software the developers of Restructure101.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        There's a blog post here on how Restructure101 was used to remove cyclic dependencies, "tangles", from Junit and a presentation from the Lausanne JUG on how it was used to remove tangles from Icefaces.



        As for the debate on whether cyclic dependencies are bad, I suggest reading Uncle Bob's Solid Principles.



        Disclaimer: I work for Headway Software the developers of Restructure101.






        share|improve this answer














        There's a blog post here on how Restructure101 was used to remove cyclic dependencies, "tangles", from Junit and a presentation from the Lausanne JUG on how it was used to remove tangles from Icefaces.



        As for the debate on whether cyclic dependencies are bad, I suggest reading Uncle Bob's Solid Principles.



        Disclaimer: I work for Headway Software the developers of Restructure101.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 22 '17 at 10:19









        Japheth Ongeri - inkalimeva

        2,39552142




        2,39552142










        answered Apr 3 '11 at 10:40









        pth - Structure101

        964




        964






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5490132%2fdealing-with-a-circular-dependency%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini