For a project at university I plan to build an annotation based tool to visualize/play around with data structures.
Here开发者_运维知识库's my idea:
Students which want to try out their self-written data structures need to:
mark the type of their data structures using some sort of marker annotation e.g.
@List public class MyList<E> { ... }
so that I know how to represent the data structure
need to provide an iterator so that I can retrieve the elements in the right order
need to annotate methods for insertion and removal, e.g.
@add public boolean insert(E e) { ... }
so that I can "bind" that method to some button.
Do similar applications exist? I googled a little bit around but didn't find anything like that.
I do not really understand why do you need to use Annotations for such a task? You will end up with some weird methods that your framework would not be able to parse, or will parse it the wrong way - bringing you to making up a set of rules how to properly write that methods so your framework could understand them.
I think it is much better solution to use interfaces instead. Create your own interface called StudentList or something, with methods like getIterator, getIndex, etc. and make students implement this interface. Then you will be able to load any of that classes and use it based on the interface they implement.
Also, you could just reuse avialable interfaces like List or Collection, however, this could make students write a lot of unused code to implement that interfaces. So I would rather go with writing my own interface.
Example of such interface would be:
interface StudentList<T> {
public Iterator<T> getIterator();
public T get(int index);
public void add(T element);
public void remove(T element);
}
Students would implement it like this (this example just wraps around ArrayList):
class MyList<T> implements StudentList<T> {
private ArrayList<T> realList;
public MyList() {
realList = new ArrayList<T>();
}
public void add(T element) {
realList.add(element);
}
.......
}
And you would test it like this:
void testList(StudentList<String> list) {
list.add(5);
list.add(10);
list.get(20);
}
testList(new MyList<String>());
Everything is clear both for students and for you this way.
精彩评论