97 lines
2.8 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/doubledispatch/DoubleDispatch.java
2016-12-30 17:23:13 -08:00
// (c)2017 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
2016-01-25 18:05:55 -08: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 {
@Override
2015-11-03 12:00:44 -08:00
public boolean add(Aluminum a) {
2015-06-15 17:47:35 -07:00
return addIt(a);
}
}
class PaperBin extends TypedBin {
@Override
2015-11-03 12:00:44 -08:00
public boolean add(Paper a) {
2015-06-15 17:47:35 -07:00
return addIt(a);
}
}
class GlassBin extends TypedBin {
@Override
2015-11-03 12:00:44 -08:00
public boolean add(Glass a) {
2015-06-15 17:47:35 -07:00
return addIt(a);
}
}
class CardboardBin extends TypedBin {
@Override
2015-11-03 12:00:44 -08:00
public boolean add(Cardboard a) {
2015-06-15 17:47:35 -07:00
return addIt(a);
}
}
class TrashBinSet {
2015-11-03 12:00:44 -08:00
private List<TypedBin> binSet = Arrays.asList(
2015-06-15 17:47:35 -07:00
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))
System.err.println("Couldn't add " + t);
2015-11-03 12:00:44 -08:00
});
2015-06-15 17:47:35 -07:00
}
2015-11-03 12:00:44 -08:00
public List<TypedBin> binSet() { return binSet; }
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<>();
2015-06-15 17:47:35 -07:00
TrashBinSet bins = new TrashBinSet();
// ParseTrash still works, without changes:
2015-11-03 12:00:44 -08:00
ParseTrash.fillBin("doubledispatch", bin);
2015-06-15 17:47:35 -07:00
// Sort from the master bin into the
// individually-typed bins:
bins.sortIntoBins(bin);
// Perform sumValue for each bin...
2017-01-20 21:30:44 -08:00
bins.binSet()
.forEach(tb -> Trash.sumValue(tb.v));
2015-06-15 17:47:35 -07:00
// ... and for the master bin
Trash.sumValue(bin);
}
2015-09-07 11:44:36 -06:00
}
2016-07-20 06:32:39 -06:00
/* Output: (First and Last 10 Lines)
2016-07-22 14:45:35 -06:00
Loading patterns.doubledispatch.Glass
Loading patterns.doubledispatch.Paper
Loading patterns.doubledispatch.Aluminum
Loading patterns.doubledispatch.Cardboard
weight of patterns.doubledispatch.Aluminum = 89.0
weight of patterns.doubledispatch.Aluminum = 76.0
weight of patterns.doubledispatch.Aluminum = 25.0
weight of patterns.doubledispatch.Aluminum = 34.0
weight of patterns.doubledispatch.Aluminum = 27.0
weight of patterns.doubledispatch.Aluminum = 18.0
...________...________...________...________...
weight of patterns.doubledispatch.Aluminum = 93.0
weight of patterns.doubledispatch.Glass = 93.0
weight of patterns.doubledispatch.Paper = 80.0
weight of patterns.doubledispatch.Glass = 36.0
weight of patterns.doubledispatch.Glass = 12.0
weight of patterns.doubledispatch.Glass = 60.0
weight of patterns.doubledispatch.Paper = 66.0
weight of patterns.doubledispatch.Aluminum = 36.0
weight of patterns.doubledispatch.Cardboard = 22.0
2015-06-15 17:47:35 -07:00
Total value = 1086.0599818825722
2015-09-07 11:44:36 -06:00
*/