Dependency Injection in the context of two simple classes












0















I have been having issues grasping Dependency Injection(or let me say its benefit). So I decided to write two simple pieces of code of one without DI and the other with it.



So I have a class A



public class A {
public void foo(){
B b = new B();
b.fooB();
}
}


as can be seen above A depends on B, B which is



public class B {
public void fooB(){
Log.e("s", "y");
}
}


and we can use A like



public void do(){
A a = new A();
a.foo();
}


But it's said that A should not simply initialize B because it depends on it, however we should have have a service that have some sort of contracts between the two classes. For Example, please if I am wrong kindly let me know



So lets have an interface BService



public interface BService {
void fooB();
}


And B becomes DiB



public class DiB implements BService {
@Override
public void fooB(){
Log.e("s", "y");
}
}


And A becomes DiA



public class DiA {
BService bService;

public DiA(BService bService){
this.bService = bService;
}

public void foo(){
bService.fooB();
}
}


and we can use A like



public void dIdo(){
BService service = new diB();
diA a = new diA(service);
a.foo();
}


So I read benefits of DI are :




  • Testable codes : Because I can actually test both codes in JUnit(I
    dont want to post the test here to avoid long question)

  • Decoupling: Its said that if class B changes then A shouldn't be
    affected, and I cant grasp that because If i change fooB() in class B
    to fooB2(), i will have to change the override method in BService
    which in turn means i will have to change it in class A


Both codes seems to work fine and I cant fathom benefit of one over the other, only that the other is more complex. So please can you enlighten me more on the benefits in the context of this simple A and B classes. What am I not getting?










share|improve this question













migrated from superuser.com Jan 23 at 17:19


This question came from our site for computer enthusiasts and power users.























    0















    I have been having issues grasping Dependency Injection(or let me say its benefit). So I decided to write two simple pieces of code of one without DI and the other with it.



    So I have a class A



    public class A {
    public void foo(){
    B b = new B();
    b.fooB();
    }
    }


    as can be seen above A depends on B, B which is



    public class B {
    public void fooB(){
    Log.e("s", "y");
    }
    }


    and we can use A like



    public void do(){
    A a = new A();
    a.foo();
    }


    But it's said that A should not simply initialize B because it depends on it, however we should have have a service that have some sort of contracts between the two classes. For Example, please if I am wrong kindly let me know



    So lets have an interface BService



    public interface BService {
    void fooB();
    }


    And B becomes DiB



    public class DiB implements BService {
    @Override
    public void fooB(){
    Log.e("s", "y");
    }
    }


    And A becomes DiA



    public class DiA {
    BService bService;

    public DiA(BService bService){
    this.bService = bService;
    }

    public void foo(){
    bService.fooB();
    }
    }


    and we can use A like



    public void dIdo(){
    BService service = new diB();
    diA a = new diA(service);
    a.foo();
    }


    So I read benefits of DI are :




    • Testable codes : Because I can actually test both codes in JUnit(I
      dont want to post the test here to avoid long question)

    • Decoupling: Its said that if class B changes then A shouldn't be
      affected, and I cant grasp that because If i change fooB() in class B
      to fooB2(), i will have to change the override method in BService
      which in turn means i will have to change it in class A


    Both codes seems to work fine and I cant fathom benefit of one over the other, only that the other is more complex. So please can you enlighten me more on the benefits in the context of this simple A and B classes. What am I not getting?










    share|improve this question













    migrated from superuser.com Jan 23 at 17:19


    This question came from our site for computer enthusiasts and power users.





















      0












      0








      0








      I have been having issues grasping Dependency Injection(or let me say its benefit). So I decided to write two simple pieces of code of one without DI and the other with it.



      So I have a class A



      public class A {
      public void foo(){
      B b = new B();
      b.fooB();
      }
      }


      as can be seen above A depends on B, B which is



      public class B {
      public void fooB(){
      Log.e("s", "y");
      }
      }


      and we can use A like



      public void do(){
      A a = new A();
      a.foo();
      }


      But it's said that A should not simply initialize B because it depends on it, however we should have have a service that have some sort of contracts between the two classes. For Example, please if I am wrong kindly let me know



      So lets have an interface BService



      public interface BService {
      void fooB();
      }


      And B becomes DiB



      public class DiB implements BService {
      @Override
      public void fooB(){
      Log.e("s", "y");
      }
      }


      And A becomes DiA



      public class DiA {
      BService bService;

      public DiA(BService bService){
      this.bService = bService;
      }

      public void foo(){
      bService.fooB();
      }
      }


      and we can use A like



      public void dIdo(){
      BService service = new diB();
      diA a = new diA(service);
      a.foo();
      }


      So I read benefits of DI are :




      • Testable codes : Because I can actually test both codes in JUnit(I
        dont want to post the test here to avoid long question)

      • Decoupling: Its said that if class B changes then A shouldn't be
        affected, and I cant grasp that because If i change fooB() in class B
        to fooB2(), i will have to change the override method in BService
        which in turn means i will have to change it in class A


      Both codes seems to work fine and I cant fathom benefit of one over the other, only that the other is more complex. So please can you enlighten me more on the benefits in the context of this simple A and B classes. What am I not getting?










      share|improve this question














      I have been having issues grasping Dependency Injection(or let me say its benefit). So I decided to write two simple pieces of code of one without DI and the other with it.



      So I have a class A



      public class A {
      public void foo(){
      B b = new B();
      b.fooB();
      }
      }


      as can be seen above A depends on B, B which is



      public class B {
      public void fooB(){
      Log.e("s", "y");
      }
      }


      and we can use A like



      public void do(){
      A a = new A();
      a.foo();
      }


      But it's said that A should not simply initialize B because it depends on it, however we should have have a service that have some sort of contracts between the two classes. For Example, please if I am wrong kindly let me know



      So lets have an interface BService



      public interface BService {
      void fooB();
      }


      And B becomes DiB



      public class DiB implements BService {
      @Override
      public void fooB(){
      Log.e("s", "y");
      }
      }


      And A becomes DiA



      public class DiA {
      BService bService;

      public DiA(BService bService){
      this.bService = bService;
      }

      public void foo(){
      bService.fooB();
      }
      }


      and we can use A like



      public void dIdo(){
      BService service = new diB();
      diA a = new diA(service);
      a.foo();
      }


      So I read benefits of DI are :




      • Testable codes : Because I can actually test both codes in JUnit(I
        dont want to post the test here to avoid long question)

      • Decoupling: Its said that if class B changes then A shouldn't be
        affected, and I cant grasp that because If i change fooB() in class B
        to fooB2(), i will have to change the override method in BService
        which in turn means i will have to change it in class A


      Both codes seems to work fine and I cant fathom benefit of one over the other, only that the other is more complex. So please can you enlighten me more on the benefits in the context of this simple A and B classes. What am I not getting?







      java dependencies






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 23 at 9:16









      BolajiBolaji

      6617




      6617




      migrated from superuser.com Jan 23 at 17:19


      This question came from our site for computer enthusiasts and power users.









      migrated from superuser.com Jan 23 at 17:19


      This question came from our site for computer enthusiasts and power users.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          on a first note, I don't agree with your "it's said that A should not simply initialize B because it depends on it".. because the same code worked for me in .NET



              class Program
          {
          static void Main(string args)
          {
          A aobj = new A();
          aobj.foo();
          }
          }
          public class A {
          public void foo()
          {
          B bojb = new B();
          bojb.fooB();
          Console.WriteLine("From A.foo() ..");
          Console.ReadLine();
          }

          }
          public class B {
          public void fooB()
          {
          Console.WriteLine("From B.fooB() ..");
          Console.ReadLine();
          }
          }


          Apart of this, guess that you are confused with your fundamentals of Dependency Injection. The dependency word itself conveys the source of injection for any class.




          1. First, the dependency can be identified during the instance of a class and the constructor is the first type.

          2. Second, once the object is instantiated, before we call any method, there will be some assignments for the properties or public fields. Although it is optional, but, the sequence is as such. Thus, the second type.

          3. Finally, the method injection.


          Recommend you to read more before you make a case.






          share|improve this answer
























          • I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

            – Bolaji
            Jan 23 at 11:17











          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%2f54332481%2fdependency-injection-in-the-context-of-two-simple-classes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          on a first note, I don't agree with your "it's said that A should not simply initialize B because it depends on it".. because the same code worked for me in .NET



              class Program
          {
          static void Main(string args)
          {
          A aobj = new A();
          aobj.foo();
          }
          }
          public class A {
          public void foo()
          {
          B bojb = new B();
          bojb.fooB();
          Console.WriteLine("From A.foo() ..");
          Console.ReadLine();
          }

          }
          public class B {
          public void fooB()
          {
          Console.WriteLine("From B.fooB() ..");
          Console.ReadLine();
          }
          }


          Apart of this, guess that you are confused with your fundamentals of Dependency Injection. The dependency word itself conveys the source of injection for any class.




          1. First, the dependency can be identified during the instance of a class and the constructor is the first type.

          2. Second, once the object is instantiated, before we call any method, there will be some assignments for the properties or public fields. Although it is optional, but, the sequence is as such. Thus, the second type.

          3. Finally, the method injection.


          Recommend you to read more before you make a case.






          share|improve this answer
























          • I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

            – Bolaji
            Jan 23 at 11:17
















          0














          on a first note, I don't agree with your "it's said that A should not simply initialize B because it depends on it".. because the same code worked for me in .NET



              class Program
          {
          static void Main(string args)
          {
          A aobj = new A();
          aobj.foo();
          }
          }
          public class A {
          public void foo()
          {
          B bojb = new B();
          bojb.fooB();
          Console.WriteLine("From A.foo() ..");
          Console.ReadLine();
          }

          }
          public class B {
          public void fooB()
          {
          Console.WriteLine("From B.fooB() ..");
          Console.ReadLine();
          }
          }


          Apart of this, guess that you are confused with your fundamentals of Dependency Injection. The dependency word itself conveys the source of injection for any class.




          1. First, the dependency can be identified during the instance of a class and the constructor is the first type.

          2. Second, once the object is instantiated, before we call any method, there will be some assignments for the properties or public fields. Although it is optional, but, the sequence is as such. Thus, the second type.

          3. Finally, the method injection.


          Recommend you to read more before you make a case.






          share|improve this answer
























          • I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

            – Bolaji
            Jan 23 at 11:17














          0












          0








          0







          on a first note, I don't agree with your "it's said that A should not simply initialize B because it depends on it".. because the same code worked for me in .NET



              class Program
          {
          static void Main(string args)
          {
          A aobj = new A();
          aobj.foo();
          }
          }
          public class A {
          public void foo()
          {
          B bojb = new B();
          bojb.fooB();
          Console.WriteLine("From A.foo() ..");
          Console.ReadLine();
          }

          }
          public class B {
          public void fooB()
          {
          Console.WriteLine("From B.fooB() ..");
          Console.ReadLine();
          }
          }


          Apart of this, guess that you are confused with your fundamentals of Dependency Injection. The dependency word itself conveys the source of injection for any class.




          1. First, the dependency can be identified during the instance of a class and the constructor is the first type.

          2. Second, once the object is instantiated, before we call any method, there will be some assignments for the properties or public fields. Although it is optional, but, the sequence is as such. Thus, the second type.

          3. Finally, the method injection.


          Recommend you to read more before you make a case.






          share|improve this answer













          on a first note, I don't agree with your "it's said that A should not simply initialize B because it depends on it".. because the same code worked for me in .NET



              class Program
          {
          static void Main(string args)
          {
          A aobj = new A();
          aobj.foo();
          }
          }
          public class A {
          public void foo()
          {
          B bojb = new B();
          bojb.fooB();
          Console.WriteLine("From A.foo() ..");
          Console.ReadLine();
          }

          }
          public class B {
          public void fooB()
          {
          Console.WriteLine("From B.fooB() ..");
          Console.ReadLine();
          }
          }


          Apart of this, guess that you are confused with your fundamentals of Dependency Injection. The dependency word itself conveys the source of injection for any class.




          1. First, the dependency can be identified during the instance of a class and the constructor is the first type.

          2. Second, once the object is instantiated, before we call any method, there will be some assignments for the properties or public fields. Although it is optional, but, the sequence is as such. Thus, the second type.

          3. Finally, the method injection.


          Recommend you to read more before you make a case.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 at 11:08









          DSK ChakravarthyDSK Chakravarthy

          11




          11













          • I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

            – Bolaji
            Jan 23 at 11:17



















          • I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

            – Bolaji
            Jan 23 at 11:17

















          I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

          – Bolaji
          Jan 23 at 11:17





          I am not making a case neither am I not saying DI is not good. I already said I just said i want to know the benefits. And thumbs up for highlighting the 3ways to go about DI.

          – Bolaji
          Jan 23 at 11:17




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54332481%2fdependency-injection-in-the-context-of-two-simple-classes%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







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?