I have looked over the suggested list of similar questions and don't really see anything that helped me so I am going to ask this...I hope you can understand it, its hard to explain (and I have simplified it to get at what I am looking for). Basically, I think this design has some redundant components, and I would like to know if their is a more appropriate way to do this.
I have 2 classes that have some similarities and some differences. One important thing that ties them together is they both MUST implement an assign() method...that warrants an Assignable interface (I think):
public interface Assignable <T extends Assignment>{
public void assign (T assn);
}
public class WorkerClassA implements Assignable<AssnAtype>{
AssnAtype assignment;
public void report() {//reports on something}
public void assign (AssnAtype assn) {assignment = assn;}
}
public class WorkerClassB implements Assignable<AssnBtype>{
AssnBtype assignment;
public void discovers() {//discovers something}
public void assign (AssnBtype assn) {assignment = assn;}
}
Note that at this point, the classes look pretty similar. Besides the fact that WorkerClassA reports() while WorkerClassB discovers(), they both assign(). I used a generic in the interface because WorkerClassA assigns an AssnAtype, but WorkerClassB assigns an AssnBtype. So I also have an Assignment super class that doesn't do much.
public class Assignment {//stuff that isn't relavent}
Now, there are other parts of the system that use the different parts of WorkerClassA and WorkerClassB - you can think of a WorkerClassA processor that does something with what is reported, and a WorkerClassB processor that does something with what is discovered.
But there is also a part of the system that simply wants to track the Workers...in fact, it wants to track anything that is Assignable. I can't figure out how to do that...so instead, I create a Worker superclass and let WorkerClassA and WorkerClassB extend that. Then I can store all the Workers is a list:
public class Worker {
String name;
public Worker(String name) {this.name = name;}
public String getName() {return name;}
public String toString() {return this.getName();}
}
And now I adjust my Worker subclasses to extend this:
public class WorkerClassA extends Worker implements Assignable<AssnAtype>{
AssnAtype assignment;
public WorkerClassA(String name) {super(name);}
public void report() {//reports on something}
public void assign (AssnAtype assn) {assignment = assn;}
}
public class WorkerClassB extends Worker implements Assignable<AssnBtype>{
AssnBtype assignment;
public WorkerClassB(String name) {super(name);}
public void report() {//reports on something}
public void assign (AssnBtype assn) {assignment = assn;}
}
So that I can store all the workers in a list:
public static void main(String[] args) {
WorkerClassA reporter = new WorkerClassA("Charlie");
reporter.assign(new AssnAtype());
WorkerClassB inventor = new WorkerClassB("Sally");
inventor.assign(new AssnBtype());
ArrayList<Worker> workerList= new ArrayList<Worker>();
workerList.add(reporter);
workerList.add(inventor);
for (Worker w : workerList) {
System.out.println(w);
}
}
I would much prefer to simple store everything assignable in the list and not create the Worker superclass but I don't think I can (other things that aren't Workers could alos be assignable I guess, but I don't have that intention...regardless, I guess that's a reason I shouldn't/can't do that?). Is this then best I can do (ie is this the best way to force my Worker classes to be assignable while allowing them to assign different subtypes of things, and making sure I can store them all in one list)?