What design pattern to use when multiple inheritance is required in java

I have a vehicle class and a beautiful ship and plane to check safety, each class implements its own safetycheck. The 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 the seaplane was needed that duplicated Ship and Plane safety checks.

 class SeaPlane implements Vehicle { @Override public void safetyCheck() { //check if oxygen mask is in place. // && //check if number of lifeboats >= number of passengers } } 

What design patterns help in such specific scenarios reduce code redundancy and simplify implementation?

+7
java design-patterns
source share
2 answers

Without creating a new interface or class for this case, you can use the principle

+7
source share

You can apply a strategy template to separate some component behavior from component definitions. You can then use these behaviors in several classes to avoid redundancy.

+1
source share

All Articles