Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
AKA: Cursor
public interface Iterator {
public abstract hasNext();
public abstract next();
public abstract remove();
}
Composes objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
public interface Graphic{
public abstract void draw();
public abstract void add(Graphic g);
}
Composite II
public class Picture implements Graphic{
final List<Graphic> graphics;
public void draw(){
for(final Graphic g : graphics){
g.draw();
}
}
public void add(final Graphic g){
graphics.add(g);
}
}
public class Rectangle implements Graphic{
// [...]
}
public class Line implements Graphic{
// [...]
}
public class Text implements Graphic{
// [...]
}
A composite type necessarily represents a union of interfaces. This means that only a subset of the interface might be valid at a given time. Accordingly, the type system becomes much less useful and runtime checks will be required.