I have a class called a Plane
.
class Plane {
private int _planeId;
public int getModel() { }
public int getNumberOfPassengers() {}
}
And I another class called PlaneService which depends on plane and adds some logic to it,
class PlaneService {
private Plane _plane;
public PlaneService(Plane _plane) {
this._plane = _plane;
}
public void validdatePlane(int planeId) {
fetch plane from planeId from db.
if ( plane.getNumberOfPassengers() is in range of {100 to 300} )
plane is valid.
}
}
But now a new requirement comes in: plane must be extended to support fighter jets. So new design looks like this:
class Plane {
private int _planeId;
public int getModel() { }
}
class PassengerPlane extends Plane {
public int getNumberOfPassengers() {}
}
class FigherJet extends Plane {
public boolean isCommissioned() {}
}
My question is how can I best design 'PlaneSvc'in OOP way ? Is there a good design pattern ?
Currently, my code looks like this:
class PlaneService {
private Plane _plane;
public PlaneService(Plane _plane) {
this._plane = _plane;
}
public void validdatePlane(int planeId) {
fetch CommercialPlane from planeId from db.
if (commericialPlaneObject != null) {
if ( plane.getNumberOfPassengers() is in range of {100 to 300} )
plane is valid.
}
fetch FighterPlaneObject from planeId from db.
if (FighterPlaneObject != null) {
if (fighterplane.isCommissioned()) {
return validPlane;
}
}
}
}
I am sure there is some design pattern to deal with such a case. I need to understand a cleaner approach to if-else here.
Aucun commentaire:
Enregistrer un commentaire