tags:

views:

118

answers:

3

Lately I've been considering creating a sort of "toolbox" for myself in java. It would consist of standard algorithms and data structures that are frequently used, but a pain to implement. (I'm thinking graphs, trees, etc)

What would be the best way to do this? I was thinking about either using generics or using an abstract class. This way I could have any object that wanted to use the methods simply provide a getGraphData() method or something similar.

I think that generics might be better though because an abstract class would force me to inherit from it, which could become problematic.

A: 

two ways to go about it:

Way 1:

  1. Create a class Graph
  2. Create methods in the Graph class, each corresponding to an algorithm of yours.

Way 2:

  1. Create a class Graph (purely a data structure)
  2. Create a class Algorithm
  3. Subclass Algorithm to define each new algorithm of yours.

Of course, it would best if you look at the STL algorithms and model your graph algorithms in a similar fashion. You could even open source your data structures and assocaited algorithms. This way the community can take care of newer algos, effeciency considerations, integration into/with existing libraries etc.

jrh.

Here Be Wolves
+3  A: 

This is called a library...

General purpose stuff should be in the Java library. If some stuff that you want is missing, you might try additional libraries such as Google Collections. You might also want domain-specific libraries.

Tom Hawtin - tackline
Well this was meant as more of a learning experience, but I generally find myself needing algorithms for my school projects and it would be nice to just have my own library, rather than using multiple ones. Also, most of our projects, we're only allowed to use code we write.
samoz
An important lesson they don't teach you at school is that unless you have a very good reason to 'roll your own', don't. It is always harder than you think, has large maintenance overheads and will almost always be inferior to the open source libraries. This is simply because your library won't get the exposure and bug fixes a good open source library does.
Nick Holt
Well the very good reason is that our classes don't allow us to use code that's not boilerplate from the teacher, standard library, or code that we've written ourselves.
samoz
In that situation I'd keep copies of previous projects then cherry pick and refactor from them as necessary when new project arise. Another lesson that they don't teach at school is that to make something generic is much harder than making it specific. Saying that, that might not apply so much with data structures. Without knowing more about your project I'd still advise sticking to the standard API and focusing on the problem in hand rather than being distracted by building your own libraries.
Nick Holt
A: 

Most of the time, one should use pre-existing libraries written by people who really know what they are doing -- those libraries have been thoroughly tested, and are implemented with the efficient algorithms to achieve good performance.

However, if one does go about making his or her own libraries, I would say try to emulate the best -- take a look at how the Java Collections and the Google Collections are designed. Look at what kind of interfaces are provided (List, Map, etc.) and what kind of abstract classes and implementations exist.

Then, if it's for educational purposes, go ahead and write your own implementations. It should be fun and exciting and definitely challenging. Try to see if the behavior is as expected -- write tests to be sure to cover the typical use, and the edge cases.

However, I'd recommend against using it for real use other than educational use. It's not a big deal to make mistakes while in school, but it's a much different story once out in the real world.

coobird