示例代码一:
package model2; import java.util.Enumeration; import java.util.Vector; /** 垃圾桶 */ class TrashBin extends Vector{ Class trashBinType; TrashBin(Class trashBinType){ this.trashBinType = trashBinType; } //争夺垃圾 boolean grab(Trash t){ if (t.getClass().equals(trashBinType)) { addElement(t); return true; } return false; } } /** 垃圾桶集合 */ class TrashBinList extends Vector{ //调用每个垃圾桶,去争夺 boolean sort(Trash t){ Enumeration e = elements(); while (e.hasMoreElements()) { TrashBin bin = (TrashBin) e.nextElement(); if (bin.grab(t)) return true; } return false; } //①: 带垃圾的垃圾桶传进来,调用每个垃圾桶,去争夺 void sortBin(TrashBin bin){ Enumeration e = bin.elements(); while (e.hasMoreElements()) if(!sort((Trash)e.nextElement())) System.out.println("Bin not found"); } } public class RecycleB { //用于接收垃圾的垃圾桶 static TrashBin bin = new TrashBin(Trash.class); public static void main(String[] args) { //接收垃圾 //ParseTrash.fillBin("Trash.dat",bin); //初始化垃圾桶集合(大号垃圾桶),并存入所有类型垃圾桶 TrashBinList trashBins = new TrashBinList(); // trashBins.addElement(new TrashBin(Aluminum.class)); // trashBins.addElement(new TrashBin(Paper.class)); // trashBins.addElement(new TrashBin(Glass.class)); // trashBins.addElement(new TrashBin(Cardboard.class)); //调用 ① trashBins.sortBin(bin); //枚举大号垃圾桶 Enumeration e = trashBins.elements(); while (e.hasMoreElements()) { TrashBin b = (TrashBin) e.nextElement(); Trash.sumValue(b); } Trash.sumValue(bin); } }
示例代码二:
package model2; import java.lang.reflect.Constructor; import java.util.Enumeration; import java.util.Vector; public abstract class Trash { private double weight; public Trash() { } Trash(double weight) { this.weight=weight; } public abstract double value(); public double weight(){ return weight; } public static void sumValue(Vector bin){ Enumeration e = bin.elements(); double val = 0.0f; while(e.hasMoreElements()){ Trash t = (Trash)e.nextElement(); val += t.weight() * t.value(); System.out.println(t.getClass().getName()+"="+t.weight()); } System.out.println("Total value="+val); } public static class PrototypeNotFoundException extends Exception{//... } public static class CannotCreateTrashException extends Exception{//... } private static Vector trashTypes = new Vector(); public static Trash factory(Info info) throws PrototypeNotFoundException,CannotCreateTrashException{ //take for(int i=0;i<trashTypes.size();i++){ Class tc = (Class)trashTypes.elementAt(i); if (tc.getName().indexOf(info.className) !=-1) { try { Constructor ctor = tc.getConstructor(new Class[]{double.class}); Trash trash =(Trash)ctor.newInstance(new Object[]{new Double(info.data)}); return trash; } catch (Exception e) { e.printStackTrace(); throw new CannotCreateTrashException(); } } } //add try { trashTypes.addElement(Class.forName(info.className)); } catch (Exception e) { e.printStackTrace(); throw new PrototypeNotFoundException(); } //back return factory(info); } public static class Info{ public String className; public double data; public Info(String className,double data){ this.className = className; this.data = data; } } }
原图: