To share TaskKey among Tasks, which one is better? Are there other better way?
way1 share by a key object.In this way, The client code(Task Queue) is easier to write to config the task table. But every task has to waste memory to store the TaskKey object.
class TaskKey {
int key1;
int key2;
// key3...
TaskKey(int key1, int key2) {
this.key1 = key1;
this.key2 = key2;
}
}
abstract class Task implements Cloneable {
TaskKey key;
int taskData;
Task(TaskKey key) {
this.key = key;
}
int getKey1() {
return key.key1;
}
int getKey2() {
return key.key2;
}
Task newInstance(int taskData) {
Task task = (Task) clone();
task.taskData = taskData;
return task;
}
abstract void doSomething();
}
class TaskQueue {
Task[][] taskTable;
void addTask(Task task) {
taskTable[task.getKey1()][task.getKey2()] = task;
}
void config() {
addTask(new Task(new TaskKey(1, 1)) {
void doSomething() {}
});
addTask(new Task(new TaskKey(1, 2)) {
void doSomething() {}
});
addTask(new Task(new TaskKey(2, 1)) {
void doSomething() {}
});
}
Queue<Task> queue;
void put(int key1, int key2, int taskData) {
Task task = taskTable[key1][key2];
queue.add(task.newInstance(taskData));
}
}
way2 share by the task class.In this way, The client code(Task Queue) is troublesomely to write to config the task table(to override the getKey() method of Task). But every task do not need to waste memory to store the TaskKey object, because the key information are shared by the concrete Task class.
abstract class Task implements Cloneable {
int taskData;
abstract int getKey1();
abstract int getKey2();
abstract void doSomething();
Task newInstance(int taskData) {
Task task = (Task) clone();
task.taskData = taskData;
return task;
}
}
class TaskQueue {
Task[][] taskTable;
void addTask(Task task) {
taskTable[task.getKey1()][task.getKey2()] = task;
}
void config() {
addTask(new Task() {
int getKey1() { return 1; }
int getKey2() { return 1; }
void doSomething() {}
});
addTask(new Task() {
int getKey1() { return 1; }
int getKey2() { return 2; }
void doSomething() {}
});
addTask(new Task() {
int getKey1() { return 2; }
int getKey2() { return 1; }
void doSomething() {}
});
}
Queue<Task> queue;
void put(int key1, int key2, int taskData) {
Task task = taskTable[key1][key2];
queue.add(task.newInstance(taskData));
}
}
another way1 and way2
interface TaskExecutor {
void exec(Task task);
}
class TaskKey {
int key1;
int key2;
int key3;
TaskExecutor executor;
}
class Task /*another way1*/ {
TaskKey key;
int taskData;// in fact it is not only a int
int getKey1() { // because i need to retrieve key1 from a task
return key.key1;
}
// also get methods for key2 key3
void exec(){
key.executor.exec(this);
}
}
class Task /*another way2*/ {
public final int key1;
public final int key2;
public final int key3;
private final TaskExecutor executor;
int taskData;
void exec(){
executor.exec(this);
}
}
You probably don’t want to do it the second way. For something as simple as storing two
ints, reflection seems to be a bit overkill. The amount of memory performance you get from this is very small, and jvm will have to work that much harder to figure out your method overloading. It’ll end up taking up a lot more code space (where do you think the java compiler stores all this extra information?) as well as slowing down execution.The first way is one way of doing it, but if it’s something as simple as storing two
ints you might consider just having the two keys in yourTaskclass instead of having a separate key.However, it seems like you don’t need to do this considering you already have a
Task[][]storing all this data, unless if you need to get the values ofkey1andkey2from a specificTaskobject, you can instead just directly doHowever, if you DO need to get the values of
key1andkey2from a specificTaskobject, you can instead use aHashMap<TaskKey,Task>instead of aTask[][]to store your data. That way you canthough you’d probably need to generate a
hashCode()andequals()method using your favorite IDE for both of them. This ends up being very memory efficient, with the same constant time access you have from the array.