如何通过反射减少IF-ELSE的使用?我可以得到代码示例 [英] How to reduce the usage of IF - ELSE by reflection ? Can I get the code example
问题描述
我正在尝试使用PizzaFactory Class的代码,以便我可以删除if else条件,使我的代码更具动态性。但是我不能弄清楚如何。
Pizza.java
包PizzaTrail;
import java.util.List;
//这是将由具体工厂扩展的主要抽象工厂
public abstract class Pizza {public abstract List fetchIngredients(String Type);
PizzaFactory.java
包PizzaTrail;
import java.util.List;
//这是具体的工厂
public class PizzaFactory extends Pizza
{
public static Pizza getConcretePizza(String PType)
{
披萨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
包PizzaTrail;
import java.util.ArrayList;
import java.util.List;
public class CheesePizza extends Pizza {
列表ing = new ArrayList();
@Override
public List fetchIngredients(String Type)
{
ing.add(Ingredient:Shredded Mozzarella Cheese);
ing.add(Ingredient:Peppers);
ing.add(Ingredate:Feta cheese);
ing.add(Ingredinator:Pesto);
return(ing);
}
}
}
任何人都可以帮我找到在pizzaFactory类中使用的反射,以便我可以动态调用类CheesePizza等。
为了在减少if-else使用方面回答您的问题,您可以动态地确定要使用的类例如而不是
if(PType.equals(Cheese)){
p = new CheesePizza();
您可以使用
Class.forName(PType +Pizza)。newInstance(); //可能需要一个包
(上面假设一个无参数构造函数)
,这样会删除任何切换类型的行为。
然而,我不会推荐这个。以上是很难调试的,不明显的。您的IDE可能会将这些类别标记为未被使用,并且可能会在未来的重构器中被删除。简单来说,我宁愿通过if / else序列指定可用的比萨饼的规格,一个基于字符串的切换,字符串到方法对象的映射等,而不是任何复杂的,不明显的,也许是脆弱的机制。
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?
To answer your question in terms of reducing 'if-else' usage, you could dynamically determine the class to use e.g. instead of
if (PType.equals("Cheese")) {
p=new CheesePizza();
you could use
Class.forName(PType + "Pizza").newInstance(); // might need a package
(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.
这篇关于如何通过反射减少IF-ELSE的使用?我可以得到代码示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!