It’s regarding Abstract Factory pattern.
As we know that there are 2 ways we can use i.e.
- Either create the instance of concrete class directly or
- Provide an interface by which we can access concrete class
Can someone tell the advantage/dis-advantages that if I use option 1 then these are issues that can occur or vice-versa with real time example if possible.
Thanks in advance…
If you have a concrete class, which is not subclassed, and you are confident that it will never be, then you can just instantiate it as in your option 1 – for this there is no need for a factory.
If you have an interface / abstract class with several concrete subclasses, and you want to be able to vary the implementations without tying the client to any of them – this is when you use a factory (or dependency injection).