publicinterfaceIBrakeBehavior {publicvoidbrake();}publicclassBrakeWithABSimplementsIBrakeBehavior {publicvoidbrake() {System.out.println("Brake with ABS applied"); }}publicclassBrakeimplementsIBrakeBehavior {publicvoidbrake() {System.out.println("Simple Brake applied"); }}/* Client which can use the algorithms above interchangeably */publicabstractclassCar {protectedIBrakeBehavior brakeBehavior;publicvoidapplyBrake() {brakeBehavior.brake(); }publicvoidsetBrakeBehavior(IBrakeBehavior brakeType) {this.brakeBehavior= brakeType; }}/* Client 1 uses one algorithm (Brake) in the constructor */publicclassSedanextendsCar {publicSedan() {this.brakeBehavior=newBrake(); }}/* Client 2 uses another algorithm (BrakeWithABS) in the constructor */publicclassSUVextendsCar {publicSUV() {this.brakeBehavior=newBrakeWithABS(); }}/* Using the Car Example */publicclassCarExample {publicstaticvoidmain(String[] args) {Car sedanCar =newSedan();sedanCar.applyBrake(); // This will invoke class "Brake"Car suvCar =newSUV();suvCar.applyBrake(); // This will invoke class "BrakeWithABS"// set brake behavior dynamicallysuvCar.setBrakeBehavior( newBrake() );suvCar.applyBrake(); // This will invoke class "Brake" }}