I have a vehicle class, and nice implmented Ship and Plane to check for safety, each class implementing its own safetycheck. World was good.
interface Vehicle {
public void safetyCheck();
}
class Ship implements Vehicle {
@Override
public void safetyCheck() {
//check if number of lifeboats >= number of passengers
}
}
class Plane implements Vehicle {
@Override
public void safetyCheck() {
//check if oxygen mask is in place.
}
}
But soon a hybrid called seaplane was needed which duplicated safety checks of Ship and Plane
class SeaPlane implements Vehicle {
@Override
public void safetyCheck() {
//check if oxygen mask is in place.
// &&
//check if number of lifeboats >= number of passengers
}
}
Which design patterns help in such particular scenarios to reduce code redundancy and make implementation cleaner ?
Plane,ShipandSeaPlane, with a parameterizedsafetyCheck, thenPlanecall super with first value of param,Shipcall super with another value of param andSeaPlanecall twice, one with both param. (may be using an enum for the param)