0

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)?

EnderWhite
  • 11
  • 1
  • 1
    If you just want to have a List with Assignable - just make it of type Assignable. List extends Assignable>> assignableList = new ArrayList<>(); – juwil Apr 26 '21 at 14:37
  • is @juwil's suggestion acceptable in terms of design...does it have any ptifalls, or is that what > is intended for? – EnderWhite Apr 26 '21 at 14:48
  • See https://stackoverflow.com/questions/252055/java-generics-wildcards for more details about generic wildcards. – Wim Deblauwe Apr 26 '21 at 14:58
  • 1
    You don't need the worker class, seems completely orthogonal to this problem. The ?'s have a disadvantage that you cannot add to them. List extends Assignable>> lets you retrieve an Assignabble> but it could be a List or a List. – matt Apr 26 '21 at 14:59
  • could i get some clarification @matt: I need the Worker class if I don't use Assignable>. But, as long as all I need is to retrieve the two Assignable Types, from the list, its fine to use Assignable>...also, I don't think I need List extends Assignable>>, I can just use List> if I only intend to put Assignables in it, right? – EnderWhite Apr 26 '21 at 15:21
  • The ? says you have something, but you don't know what it is. Eg. List vs List extends Number> in both cases you can say Number a = list.get(...) but you cannot add to the List extends Number> because the ? could be Number, Integer, Float, etc. – matt Apr 26 '21 at 15:33
  • List> would let you put assignables in the list, but when you get them out, you won't know what they are. Assignable> a = list.get(0); How can you call assign? – matt Apr 26 '21 at 15:45

0 Answers0