I'm trying to write a generic method which get called and based on object type it should set indicators.
- Will it break any solid principles?
- Is there a better way of doing it?
I was working on one of the features where I have 3 lines of business like Car, Van and Bike.
I have IVehicle
interface which is handling all the common implementation between the 3 lines of business.
Now, I have to set two common indicator Car
and Van
but for Bike
, I want to set some different indicator. I want to create an object of this class once but it handle based on type of line business.
If write this method like this will it break any SOLID principles? If it is incorrect, can you please suggest an alternative?
Note: INDICATOR_B
should be true in all LOB
function populateIndicator() {
if (_motorObject typeis Car or _motorObject typeis entity.Van) {
_motorObject.INDICATOR_A = true
}
if (_motorObject typeis Bike) {
_motorObject.INDICATOR_C = true
}
_motorObject.INDICATOR_B = true
}
Short version: use polymorphism to implement
populateIndicator
method.Checking types in the base class breaks Liskove substitution principle. If you have a duplicate code for assigning like
_motorObject.INDICATOR_B = true
you can use template method pattern.this design won't break any rules.