have you thought of a 2-dimensional matrix
- Producer
- Consumer
The content of the matrix defines the "stuff" they produce/consume and also if they are allowed to have an relation.
Would that work?
have you thought of a 2-dimensional matrix
The content of the matrix defines the "stuff" they produce/consume and also if they are allowed to have an relation.
Would that work?
I suppose you want to have a Supplier
class and a Consumer
class that implement generics so that you might have implement Supplier<Clothes>
or Consumer<Food>
or something else in your Warehouse
class?
You might try something along the lines of this. This is more likely implementing a generics factory, I suppose.
public class Supplier<T>{
//You might decide you need an actual constructor that does something
public Supplier(){}
public T supplyItem(){
return new T();
}
}
Consumer might look like...
public class Consumer<T>{
private int consumeCount = 0;
//You might decide you need an actual constructor that does something
public Consumer(){}
public void consumeItem(T item){
consumeCount++;
}
public int consumeCount(){
return consumeCount;
}
}
And finally, your Warehouse could include something like...
Supplier<Integer> integerSupplier = new Supplier<Integer>();
Consumer<Integer> integerConsumer = new Consumer<Integer>();
Integer i = integerSuppler.supplyItem();
integerConsumer.consumeItem(i);
integerConsumer.consumeItem(integerSupplier.supplyItem());
System.out.println(integerConsumer.consumeCount());
Which we'd expect to print "2". You might also change your consume methods to take an instance of Object
instead of T
, and use instanceOf
to either deal with it or say "Can't consume that, not my thing." There are some things you should be careful of with instanceOf
though, so if it's not required to be that robust I wouldn't worry about it. http://www.javapractices.com/topic/TopicAction.do?Id=31 has a nice explanation of why.
EDIT: It might look like Consumer and Supplier are interacting with each other, especially when you have a line like integerConsumer.consumeItem(integerSupplier.supplyItem());
, but it's important to note that the Consumer and the Supplier aren't actually interacting with each other there. the Supplier is simply generating a new Object, and the Consumer is taking that as an argument. While Warehouse knows of the existence of the Consumer and Supplier, the Consumer does not know of the existence of Supplier and vice versa.
Warehouse
public enum ITEMTYPE //known and public
Map<ITEMTYPE, count> items
Map<Supplier, ITEMTYPE> suppliers
registerSupplier(Supplier)
addItems(Supplier, count)
registerConsumer(Consumer)
consumeItems(Consumer, count)
Supplier
ITEMTYPE type
ITEMTYPE getType()
Consumer
ITEMTYPE type
ITEMTYPE getType()
The way to use it:
Warehouse w = new Warehouse()
Supplier s1 = new Supplier(ITEMTYPE pens)
w.registerSupplier(s1)
w.addItems(s1, 10) // Update the data structure in warehouse with validations
Consumer c1 = new Consumer(ITEMTYPE pens)
w.registerConsumer(c1)
w.consumeItems(c1, 5) // Update the data structure in warehouse with validations