2015-09-07 11:44:36 -06:00
|
|
|
// patterns/doubledispatch/DoubleDispatch.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
// Using multiple dispatching to handle more
|
2021-03-04 16:15:04 -07:00
|
|
|
// than one unknown type during a method call.
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java patterns.doubledispatch.DoubleDispatch}
|
2015-06-15 17:47:35 -07:00
|
|
|
package patterns.doubledispatch;
|
|
|
|
import patterns.trash.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
class AluminumBin extends TypedBin {
|
2021-03-04 16:15:04 -07:00
|
|
|
public AluminumBin() { super("Aluminum"); }
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public boolean add(Aluminum a) {
|
2015-06-15 17:47:35 -07:00
|
|
|
return addIt(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PaperBin extends TypedBin {
|
2021-03-04 16:15:04 -07:00
|
|
|
public PaperBin() { super("Paper"); }
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public boolean add(Paper a) {
|
2015-06-15 17:47:35 -07:00
|
|
|
return addIt(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GlassBin extends TypedBin {
|
2021-03-04 16:15:04 -07:00
|
|
|
public GlassBin() { super("Glass"); }
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public boolean add(Glass a) {
|
2015-06-15 17:47:35 -07:00
|
|
|
return addIt(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CardboardBin extends TypedBin {
|
2021-03-04 16:15:04 -07:00
|
|
|
public CardboardBin() { super("Cardboard"); }
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public boolean add(Cardboard a) {
|
2015-06-15 17:47:35 -07:00
|
|
|
return addIt(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TrashBinSet {
|
2021-03-04 16:15:04 -07:00
|
|
|
public final List<TypedBin> binSet =
|
|
|
|
Arrays.asList(
|
|
|
|
new AluminumBin(), new PaperBin(),
|
|
|
|
new GlassBin(), new CardboardBin()
|
|
|
|
);
|
2015-11-03 12:00:44 -08:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void sortIntoBins(List bin) {
|
|
|
|
bin.forEach( aBin -> {
|
2015-06-15 17:47:35 -07:00
|
|
|
TypedBinMember t = (TypedBinMember)aBin;
|
|
|
|
if(!t.addToBin(binSet))
|
2021-03-04 16:15:04 -07:00
|
|
|
throw new RuntimeException(
|
|
|
|
"sortIntoBins() couldn't add " + t);
|
2015-11-03 12:00:44 -08:00
|
|
|
});
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class DoubleDispatch {
|
|
|
|
public static void main(String[] args) {
|
2015-11-03 12:00:44 -08:00
|
|
|
List<Trash> bin = new ArrayList<>();
|
|
|
|
ParseTrash.fillBin("doubledispatch", bin);
|
2021-03-04 16:15:04 -07:00
|
|
|
TrashBinSet bins = new TrashBinSet();
|
2015-06-15 17:47:35 -07:00
|
|
|
// Sort from the master bin into the
|
|
|
|
// individually-typed bins:
|
|
|
|
bins.sortIntoBins(bin);
|
2021-03-04 16:15:04 -07:00
|
|
|
// Sum value of each bin...
|
|
|
|
bins.binSet.forEach(tb ->
|
|
|
|
TrashValue.sum(tb.bin(), tb.type));
|
|
|
|
// ... and for the master bin:
|
|
|
|
TrashValue.sum(bin, "Trash");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
/* Output:
|
|
|
|
Loading patterns.doubledispatch.Cardboard
|
2016-07-22 14:45:35 -06:00
|
|
|
Loading patterns.doubledispatch.Paper
|
|
|
|
Loading patterns.doubledispatch.Aluminum
|
2021-03-04 16:15:04 -07:00
|
|
|
Loading patterns.doubledispatch.Glass
|
|
|
|
Aluminum weight: 1.80 * price: 1.67 = 3.01
|
|
|
|
Aluminum weight: 3.40 * price: 1.67 = 5.68
|
|
|
|
Aluminum weight: 2.70 * price: 1.67 = 4.51
|
|
|
|
Total Aluminum value = 13.19
|
|
|
|
Paper weight: 8.00 * price: 0.10 = 0.80
|
|
|
|
Paper weight: 6.60 * price: 0.10 = 0.66
|
|
|
|
Paper weight: 9.10 * price: 0.10 = 0.91
|
|
|
|
Total Paper value = 2.37
|
|
|
|
Glass weight: 5.40 * price: 0.23 = 1.24
|
|
|
|
Glass weight: 4.30 * price: 0.23 = 0.99
|
|
|
|
Glass weight: 3.60 * price: 0.23 = 0.83
|
|
|
|
Total Glass value = 3.06
|
|
|
|
Cardboard weight: 4.40 * price: 0.11 = 0.48
|
|
|
|
Cardboard weight: 2.20 * price: 0.11 = 0.24
|
|
|
|
Cardboard weight: 1.20 * price: 0.11 = 0.13
|
|
|
|
Total Cardboard value = 0.86
|
|
|
|
Cardboard weight: 4.40 * price: 0.11 = 0.48
|
|
|
|
Paper weight: 8.00 * price: 0.10 = 0.80
|
|
|
|
Aluminum weight: 1.80 * price: 1.67 = 3.01
|
|
|
|
Glass weight: 5.40 * price: 0.23 = 1.24
|
|
|
|
Aluminum weight: 3.40 * price: 1.67 = 5.68
|
|
|
|
Cardboard weight: 2.20 * price: 0.11 = 0.24
|
|
|
|
Glass weight: 4.30 * price: 0.23 = 0.99
|
|
|
|
Cardboard weight: 1.20 * price: 0.11 = 0.13
|
|
|
|
Paper weight: 6.60 * price: 0.10 = 0.66
|
|
|
|
Aluminum weight: 2.70 * price: 1.67 = 4.51
|
|
|
|
Paper weight: 9.10 * price: 0.10 = 0.91
|
|
|
|
Glass weight: 3.60 * price: 0.23 = 0.83
|
|
|
|
Total Trash value = 19.48
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|