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