Class Libraries
They make use of open-closed principle which means that the class can be adapted / extended in derived classes (heirs) but their interface remains stable and is provided in a library for clients. Changing requirements should not require code changes. Now code is added instead in a derived class or a new implementation of an interface is contributed. What is open and what is closed has to be chosen strategically by the designer. Abstraction is key to adaptability.
// this interface allows the contribution of new implementations of it
interface IShape {
void draw(Graphics gc);
}
// the shape painer is closed to paint strategy (cannot be adapted by client or derived class)
public class ShapePainter {
private List<IShape> shapeList = new ArrayList<>();
public void addShape(IShape shape) {
shapeList.add(shape);
}
public void paintAll(Graphics gc) {
shapeList.forEach( (Ishape shape) -> shape.draw(gc) );
}
}