Open/closed Principle

Classes should be open for extension but closed for modification. Once a class is developed and tested, new behavior should be added by extending or wrapping it rather than editing it directly, so that existing clients are never broken by new features.

Examples:

An Order class with hardcoded shipping-method logic must be edited every time a new carrier is added. Extract a Shipping interface and move each method into its own class. New carriers implement the interface without touching Order:

class GroundShipping implements Shipping { ... }
class AirShipping  implements Shipping { ... }
// Add NightShipping without changing Order

Synonyms: ocp