How to create a library that stores commonly reused code - That needs adjustments from project to project?

StackOverflow https://stackoverflow.com/questions/22337045

  •  13-06-2023
  •  | 
  •  

Question

(This question is related to any kind of development, not only games).

I am relatively new to game development with Java.

I made a number of games, and started noticing that at the beginning of each project, I pretty much always rewrite very similar code and classes.

For example: The structure of the game loop, the base class for all game entities, the mechanism for getting keyboard input and moving the character accordingly, etc. All of these are very similar (but not identical) from project to project.

I realized that the best way to deal with this, is probably to create a library which will store all of this code that I constantly have to rewrite.

The problem is, I don't really know where to start.

A library is a JAR that contains a collection of classes (is this accurate?). If so, than I can't just create a BaseGameEntity class and put it in a library to use in my games.

This is because because this class is very similar from game to game I make, but not identical. I still always need to change some of it's code. So I can't have it locked up in a library.

Same thing goes for the mechanism for getting input from the keyboard to move character. I could encapsulate it in a class and put it in a library, but again - it's never identical from game to game. I still need to adjust it's code, I can't just reuse the exact same code.

What is the common solution for this? Are there common 'strategies' used for these (I assume) common cases? Or do developers use their common sense and known design patterns to achieve this, without any specific 'strategy'?

EDIT: Are there common design patterns I should learn to help me build a good reusable library?

Was it helpful?

Solution

I would recommend if you're doing any type of development in Java that you learn some design patterns. Books I find are ineffectual for non-academic programmers (i.e. those that didn't get a Computer Science degree) so your best bet is to go to the web. Here is a link to a page I still use:

http://www.fluffycat.com/Java-Design-Patterns/

Basically the problems you are facing now have already been solved, albeit in a general way, and there is a collection of those solutions somewhere. Your challenge is to identify the problem you are trying to solve in your own code, and then choose the pattern that best solves your problem. This requires being more than just nominally familiar with the patterns.

Sometimes looking at things like design patterns when you're new to programming can be daunting, so a solution in the interim for you would be to (a must-do practice in programming anyway) take common functionality in your code and break it down into separate, well-defined classes (look up OOP cohesion). For instance, if a class you've written has a method that takes a String and formats it a certain way, you might want to create a new class that just does that via a public static method, rather than keep that single purpose code inside your original class. Same thing goes with, say, a method that reads data from a properties file, or a method that deals with time, etc. This would be the first step to creating a package or starting framework that you can reuse. The goal is that if you break your code into pieces small enough, they can be used anywhere.

OTHER TIPS

You have two choices:

1- Refactor your code so that you can put some never changing pieces into a library and extract to a JAR.

2- If you can definitely not extract to a JAR because your code will always change a tiny bit, then simply create a separate Java project with the code that would ideally go in a library but that can't because of its tiny differences from game to game, and then reference the project adding it to the build path of your actual game project. Then when the next game comes around, make a copy of your original "library" project and repeat.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top