Could anyone help me to understand the the following queries regarding factory method pattern?
im refering this link as a sample.
1.what is the need of abstract class Factory in factory method pattern? What if use a normal class?
2.It is stated that change in the code will not affect the client. But if A new class product3 has been added, an appropriate change will be required in the client code also.(as per the below code). Then what is the advantage of this design pattern?
3.What is the benefit of objFactories[0] = new concreteFactoryforProduct1();
over concreteFactoryforProduct1 factory = new concreteFactoryforProduct1();
Factory[] objFactories = new Factory[2];
objFactories[0] = new concreteFactoryforProduct1();
objFactories[1] = new concreteFactoryforProduct2();
foreach (Factory objFactory in objFactories)
{
Product objProduct = objFactory.GetProduct();
objProduct.GetDetails();
}
4.Why we cant implement open close principle in simple design pattern?
(1) As per the simplest use of abstract classes, adding abstract avoids creating instances using new Factory(). (Because it is not meaningful, same as animal and elephant -> animal is only an abstraction, no entity exist called animal)
(2) It is just a demo. There will no use case on earth to list down the available factories.
(3) In the example, It's a Factory array, your question is not applicable.
(4) I think you are asking about a situation without using Factory method pattern. There can be a lot of situations. You can identify if you try to implement it.