I was trying to use reflection for the code of PizzaFactory Class so that I can remove the if else condition and make my code more dynamic. But I am not able to figure out how.
Pizza.java
package PizzaTrail;
import java.util.List;
//this is the main abstract factory which will be extended by the concrete factory
public abstract class Pizza { public abstract List fetchIngredients(String Type); }
PizzaFactory.java
package PizzaTrail;
import java.util.List;
//this is the concrete factory
public class PizzaFactory extends Pizza
{
public static Pizza getConcretePizza(String PType)
{
Pizza p=null;
if (PType.equals("Cheese"))
{
p=new CheesePizza();
} else if (PType.equals("Pepperoni"))
{
p=new PepperoniPizza();
}
else if (PType.equals("Clam"))
{
p = new CalmPizza();
}
else if (PType.equals("Veggie"))
{
p= new VeggiePizza();
}
return(p);
}
}
ChessePizza.java
package PizzaTrail;
import java.util.ArrayList;
import java.util.List;
public class CheesePizza extends Pizza {
List ing = new ArrayList();
@Override
public List fetchIngredients(String Type)
{
ing.add("Ingredient : Shredded Mozzarella Cheese");
ing.add("Ingredient : Peppers");
ing.add("Ingredient : Feta cheese");
ing.add("Ingredient : Pesto");
return (ing);
}
}
}
Can anyone help me get the reflection used in the pizzaFactory class so that i can call the class CheesePizza, etc dynamically?
You could provide the class of the concreate pizza to the factory method -
To answer your question in terms of reducing 'if-else' usage, you could dynamically determine the class to use e.g. instead of
you could use
(the above assumes a no-args constructor)
and that would remove any switching-type behaviour.
However I wouldn't recommend this. The above is difficult to debug and non-obvious. Your IDE will likely mark such classes as not being used and a possible candidate for removal during a future refactor. I would favour clarity in the vast majority of cases.
In short, I would much rather specify the specifications of available pizzas via an if/else sequence, a String-based switch, a map of strings to method objects etc. rather than any convoluted, non-obvious, perhaps fragile mechanism.
Bearing in mind that using reflection to solve a problem usually leaves you with two problems - how about using an
enum
?lets say you have your interface pizza
public interface Pizza { \*some methods *\}
and implementations such as
public class CheesePizza implements Pizza {}
you could create enum
PizzaType
now to create new fresh pizza all what you need to do is
Pizza pizza = PizzaType.Cheese.create();
You could maintain such a map:
Then, for example:
You will not be able to avoid the use of conditions with reflection, otherwise your code will fail at some point. However, given your request, you can do this:
Then, during implementation, you can determine the type of pizza you want using the if condition checks:
P.S. I have noticed that you're using Camelcase in your package name. make it a habit of using lowercase letters for packages, and where multiple words are involved, use an underscore. This is just the Java convention, though not a requirement.
I hope this works for you.