例如:下面的接口定义了组件的功能:发送消息;类Transceiver实现了该接口;而其子类Pool只是治理多个Transceiver对象,而没有提供自己的接口实现。建议使用组合方式,而不是继续! public interface ITransceiver{ public abstract send(String msg); }
public class Transceiver implements ITransceiver { public send(String msg){ System.out.println(msg); } }
//使用继续方式的实现 public class Pool extends Transceiver{ private List pool = new Vector(); public void add(Transceiver aTransceiver){ pool.add(aTransceiver); } public Transceiver get(int index){ pool.get(index); } }
//使用组合方式的实现 public class Pool { private List pool = new Vector();