design_model(8)composite

1.组合模式 //让某一组实现共同的功能

2.实例

public interface Workfile {
	public abstract void KillVirus();
}

public class Floder implements Workfile {

	@Override
	public void KillVirus() {

	}

}

class ImageFloder implements Workfile {

	@Override
	public void KillVirus() {

	}

}

public class CompositeHandler {
	private ArrayList<Workfile> f1 = new ArrayList<>();

	public void add(Workfile f) {
		f1.add(f);
	}

	public ArrayList<Workfile> getF1() {
		return f1;
	}

	public void killVirus() {
		for (Workfile workfile : f1) {
			workfile.KillVirus();
		}
	}
}

public class Client {
     public static void main(String[] args) {
		  Floder f2 = new Floder();
		  ImageFloder f3 = new ImageFloder();
		  CompositeHandler ch = new CompositeHandler();
		  ArrayList<Workfile> f1 = ch.getF1();
		  f1.add(f2);
		  f1.add(f3);
		  ch.killVirus();
	}
}

猜你喜欢

转载自www.cnblogs.com/gg128/p/9570172.html