In a scenario like below where an object needs to be intantiated based on some conditional logic, can the factory method pattern help to avoid client code getting cluttered due to number of if/elseif conditions (which would also be a maintenance nightmare if more and more products needs to get created due to different variations of logics).
Or else is there a any other design pattern that could come to rescue?
public interface IProduct
{
void Method1();
}
public class ProductA : IProduct
{
void Method1()
{
}
}
public class ProductB : IProduct
{
void Method1()
{
}
}
public class ProductC : IProduct
{
void Method1()
{
}
}
public class Client
{
public void Test()
{
int count = 5;
IProduct product;
if (count < 10)
{
product = new ProductA();
}
else if (count == 10)
{
product = new ProductB();
}
else if (count > 10)
{
product = new ProductC();
}
product.Method1();
}
}