DEV Community

Discussion on: SOLID Principles for OOP

Collapse
 
junipa profile image
Junipa

Hello. I am not sure to understand why you wrote "Now to follow the above example you'd need to add an if statement in the calling class for both object and switch between them" in the Open/Close Principle section: since both objects implement the IOrder interface, from my understanding, one could call the MakeOrder() method regardless of the concrete type of the object. Am I missing something ?

Collapse
 
mdjorov profile image
Miro

Yes, for me something is not right either. The if statement should be in a Factory class, that knows what object to return. Something like:
We have somewhere a class factory
IOrder OrderFactory(int size)
{
if (size < 100)
return new Order;

return new LargeOrder();
}

and a class using it:
class Foo ()
{
void Bar(int size)
{
IOrder order = OrderFactopy(size);
order.MakeOrder();
}
}
This way the Foo class won't change if somewhere in the future an ExtraLargeOrder class appear. The only place that will change is the OrderFactory class, but all other code won't.