How to approach this design problem

Scenario

One warehouse, suppliers and consumers. One supplier can produce only one type of material. One consumer may also consume only one type of material. The warehouse knows about suppliers and consumers, but none of them know about each other.

How can I create interfaces for all participants in this scenario and simulate it using generics to demonstrate how the warehouse works with several suppliers, consumers and various types of materials.

+3
source share
3 answers

, Supplier Consumer, , Supplier<Clothes> Consumer<Food> - Warehouse?

- . , factory.

public class Supplier<T>{
    //You might decide you need an actual constructor that does something
    public Supplier(){}

    public T supplyItem(){
        return new T();
    }
}

...

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;
    }
}

, , - ...

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());

"2". , T Object instanceOf, : " , ". , instanceOf, , , , , . http://www.javapractices.com/topic/TopicAction.do?Id=31 , .

EDIT: , , integerConsumer.consumeItem(integerSupplier.supplyItem());, , . , . Warehouse , .

+2

"", /, .

?

0

public enum ITEMTYPE //known and public
Map<ITEMTYPE, count> items
Map<Supplier, ITEMTYPE> suppliers

registerSupplier(Supplier)
addItems(Supplier, count)

registerConsumer(Consumer)
consumeItems(Consumer, count)

ITEMTYPE type
ITEMTYPE getType()

ITEMTYPE type
ITEMTYPE getType()

:

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
0

Source: https://habr.com/ru/post/1759780/


All Articles