Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (776)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  API Programming in Java?  (Read 1310 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob Pickens
« Posted 2014-03-13 23:36:14 »

I'm just going to program some basic API's for myself. I need some quick code for the WIJ Saturday. So, on that note, how do you make an API. Yes, I know how to export to a .jar. I'm talking about certain things I'm supposed to do. (Can't think of that fancy word at the moment.) Is a library just a bunch of abstract classes or is there something more to it?

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-03-14 00:57:51 »

Given that you're going to use it yourself, it can be whatever you need it to be.

From the Wikipedia: "an API specifies a set of functions or routines that accomplish a specific task or are allowed to interact with a specific software component."
That's from the procedural languages section, but it still holds true. Think about it, anything in the java standard library (or any library) is an API.

Example:

Quote from: Oracle JavaDocs
The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.

Yep, even something as basic and seemingly intrinsic to the language as Strings is an API. Some languages don't even have Strings, at least formally like this by default, you have to make the API yourself. (eg. C)
Offline TeamworkGuy2

Junior Devvie


Medals: 10



« Reply #2 - Posted 2014-03-14 00:59:49 »

Building on BurntPizza's reply, there are many concepts and structures that make a good API. People spend there lives designing good APIs.
Although that may not be the abstract answer you're looking for.

If you have time, pick up Joshua Bloch's "Effective Java" (2nd ed.), awesome, concise advice on overriding, overloading, abstract classes, interfaces, access modifiers, encapsulation, cohesion, etc. I've read through almost twice now and still learning stuff.

My personnel advice...
Make classes that are often used as parameters immutable (java's String class is a good example). A lot of methods in the Java API take strings and it's a good thing that Strings are immutable.
A example of this failing is the getDimensions() method in most Swing GUI classes.  Dimensions is a mutable class with publicly accessible .x and .y variables.

This slip on the part of Java's designers has caused headaches for developers the world over and has caused some performance problem's for Swing since every Swing class must make defensive copies of Dimensions objects when using them (Effective Java, p.235).

Higher level logic/processing classes (for example Scanner, Matcher, OutputWriter) that you don't envision being passed around as parameters, can have stateful methods, but should avoid it.
For example, a rendering class that requires startBatch() and endBatch() to be called before and after a group of render(Renderable r) calls is not really a good design.  It is easy to forget to call the methods in the right order which can end up trading valuable development time for debugging time.

For example, the class mentioned above could be consolidated to provide a renderBatch(Collection<Renderable> r) or renderBatch(Renderable[] r) method which removes the chance for the programmer to make an error when using the class.

This type of programming removes possible runtime errors and forces the programmer to program correctly and if the programmer makes a mistake, the mistake becomes a compile time error rather than a runtime error.  In one type of perfect programming world programmers could/would write code which would be impossible to compile with bugs.  In other words, null checking, bounds checking, error handling, error recovery, correct object state, etc. would all be part of the language/object syntax itself.

Such an idea is the extreme boundary of moving runtime errors to compile time errors, but in small doses the idea greatly improves API design Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #3 - Posted 2014-03-14 01:05:40 »

Indeed, proper API design is important, and something I obsess over to a likely unhealthy extent.
One thing that can help (with APIs and programming in general) is a working knowledge of good design patterns. So here is a nifty website about them, even in a gamedev context: http://gameprogrammingpatterns.com Seems like it's down right now though.
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (269 views)
2018-11-16 13:46:03

hadezbladez (146 views)
2018-11-16 13:41:33

hadezbladez (279 views)
2018-11-16 13:35:35

hadezbladez (62 views)
2018-11-16 13:32:03

EgonOlsen (2109 views)
2018-06-10 19:43:48

EgonOlsen (2141 views)
2018-06-10 19:43:44

EgonOlsen (1353 views)
2018-06-10 19:43:20

DesertCoockie (1940 views)
2018-05-13 18:23:11

nelsongames (1593 views)
2018-04-24 18:15:36

nelsongames (2241 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!