Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Is this code an API? Beginner learning to write an API and decoupling.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Is this code an API? Beginner learning to write an API and decoupling.

    Hello, I am learning about decoupling, dependency injection, etc. and so on. I believe I understand them, but I am now trying to apply them with some learning code, nothing serious. Is this an API?

    Here is a very simple example, something just for learning.

       interface Board {
           public void drawBoard();
        }
     
        public class BoardDraw implements Board {
     
           public void drawBoard(){
              System.out.println("  |  |  ");
              System.out.println("--------");
              System.out.println("  |  |  ");
              System.out.println("--------");
              System.out.println("  |  |  ");
     
           }
     
        }
     
        public class TicTacToe {
           public static void main(String args[]) {
     
     
              Board board = new BoardDraw();
              board.drawBoard();
     
     
           }
     
        }
    Ok. Another guy comes along and says he wants to draw a board with asterisks.

         public class BoardDraw implements Board {
     
               public void drawBoard(){
                  System.out.println("  *  *  ");
                  System.out.println("********");
                  System.out.println("  *  *  ");
                  System.out.println("********");
                  System.out.println("  *  *  ");
     
               }
     
            }
    - Does the new guy compile this BoardDraw class in the directory where
    main is located?
    - Does an XML file point to a location for the new
    class?
    - This is decoupled isn't it?

    Also, if I wrote the class with main and produced a document that told how to create a new BoardDraw class and that the programmer must use the Board interface, does this mean I created an "API"?

    Thanks for any help.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Is this code an API? Beginner learning to write an API and decoupling.

    The term "API" is pretty broad, so the answer to your question is... sure, depending on your definition.

    Your "API" might consist of a single Board interface and a single BoardDraw class. You might wrap them up in a jar or something. Your user might put that jar on his classpath, which would mean he could call your code from his, exactly how you use the standard Java API classes like String, ArrayList, etc.

    The programmer might want to extend your API by creating his own Board implementation- either by implementing Board or by extending your BoardDraw class. You probably shouldn't use the same class name for your API and his extension of that API, so let's call his class MyBoardDraw.

    The MyBoardDraw class might be directly in his code, or he might create his own API or library by separating that class and just making sure it's on his classpath.

    Now, maybe you have another function in your API that takes a Board argument. That function is decoupled (if I understand how you're using the word here) from the implementations of the Board class because it's using the *interface* rather than the *implementation*. So your function will still work whether a programming is using your BoardDraw class or their own MyBoardDraw class.

    How everything gets linked together is an open question, and different projects approach it differently. You might specify your classpath as xml that gets fed into a dependency injector. You might specify your classpath in some kind of build script. Or you might specify your classpath simply by feeding it into a console.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Learning2Java (April 22nd, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Is this code an API? Beginner learning to write an API and decoupling.

    How can the other guy compile his code if the interface is in my code already compiled? If he writes that board alternative above, doesn't the interface have to be in his directory (or classpath, or whatever), so he can compile his code? Can you do that with the interface that's already compiled?

    EDIT: I reread your second paragraph. I think this is what you are saying. The new programmer can read it even though it is compiled like any Java class.
    No. It is not homework. I'm just learning on my own.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Is this code an API? Beginner learning to write an API and decoupling.

    Quote Originally Posted by Learning2Java View Post
    How can the other guy compile his code if the interface is in my code already compiled? If he writes that board alternative above, doesn't the interface have to be in his directory (or classpath, or whatever), so he can compile his code? Can you do that with the interface that's already compiled?
    Think about it this way: you implement interfaces that have already been compiled every time you implement an interface from the Java API itself. Providing your own API is exactly like that: you give somebody else access to some code you wrote in the form of public methods (which might interact with other private methods you wrote).

    Of course, you *could* give the other person access to your source code. But there's nothing saying you *have* to do that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Learning2Java (April 22nd, 2014)

Similar Threads

  1. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Networking Tutorials
    Replies: 35
    Last Post: August 5th, 2014, 12:59 AM
  2. Not sure where to start: Trying to write a program dealing with an Amazon API
    By ~DrummerGirl~ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2013, 06:20 PM
  3. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 31
    Last Post: June 13th, 2011, 01:42 PM
  4. Read (using Odbc)and write using (POI api)
    By amruta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2010, 09:12 AM
  5. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM

Tags for this Thread