如何通过反射来代替N多的r ifelsee

参考下列代码:
&price code=&1& classname=&com.test.RegularPrice& /&
&price code=&2& classname=&com.test.ChildrensPrice& /&
&price code=&3& classname=&com.test.NewReleasePrice& /&
上面的为if...else... 改写版本,可放入配置文件中,然后通过解析xml文件来获取对应的price
下面代码即为通过反射来获取对应的price,代码写的很漂亮,值得学习.
import java.util.HashM
import java.util.M
public class PriceConfig {
private Class&Price&
private static Map&Integer, PriceConfig& map = new HashMap&Integer, PriceConfig&();
PriceConfig(int code, String classname) {
this.code =
this.clazz = initClass(classname);
register(this);
@SuppressWarnings(&unchecked&)
private Class&Price& initClass(String classname) {
Class&Price& clazz =
clazz = (Class&Price&)Class.forName(classname);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(classname + & isnot exist&);
if(!clazz.isAssignableFrom(Price.class)) {
throw new IllegalArgumentException(classname + & isnot Price implementation&);
private static void register(PriceConfig priceConfig) {
map.put(priceConfig.getCode(), priceConfig);
public static Price getPrice(int code) {
PriceConfig price = map.get(code);
if(price == null) {
new IllegalArgumentException(&Incorrect Price Code&);
return price.getPrice();
public int getCode() {
public Class&Price& getClassname() {
public Price getPrice() {
return (Price)clazz.newInstance();
} catch (InstantiationException e) {
throw new Xxxxxxxxx();
} catch (IllegalAccessException e) {
throw new Xxxxxxxxx();
3:以后代码直接这样就可以用了:
Price&price&=&PriceConfig.getPrice(CHILDRENS);
经过这样的改造之后就是一个符合“开-闭原则”的易扩展、低耦合的功能了,呵呵。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:27427次
排名:千里之外
原创:49篇
转载:55篇
(2)(3)(4)(3)(9)(8)(3)(2)(2)(3)(5)(5)(3)(2)(4)(4)(2)(21)(12)(2)(5)import java.lang.reflect.InvocationTargetEimport java.lang.reflect.Mimport java.util.HashMimport java.util.Mpublic class ReflectInstance {/** *
* @param classPath
classpath * @param methodName
methodName * @param args
its type can be (String,Integer,Boolean,Map,List) */@SuppressWarnings("unchecked")public static Map&String,String& sendMsg(String classPath,String methodName,Map&String,String& args1,Map&String,Object& args2) {try {Class&?& simpleClass = Class.forName(classPath);Method simpleMethod =Object simpelObject = simpleClass.newInstance();simpleMethod = simpleClass.getDeclaredMethod(methodName, Map.class,Map.class);simpleMethod.setAccessible(true);Map&String,String& result = (Map&String, String&) simpleMethod.invoke(simpelObject, args1,args2);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}public static void main(String[] args)
{Map map1 = new HashMap();map1.put("aa", "kill me!");Map map2 = new HashMap();map2.put("bb", " don't kill me !");sendMsg("mon.ReflectInstance","sendMsg",map1,map2);}public Map&String,String& sendMsg(Map&String,String& args,Map&String,Object& args2){System.out.println("order-----------"+args);Map&String,String& map = new HashMap&String,String&();map.put("test", args.get("aa"));}}
最新教程周点击榜
微信扫一扫反射机制解决多重ifelse base语句 -
- ITeye技术网站
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
import java.util.HashM
import java.util.M
public class ReflectInstance {
* @param classPath
* @param methodName
methodName
* @param args
its type can be (String,Integer,Boolean,Map,List)
@SuppressWarnings("unchecked")
public static Map&String,String& sendMsg(String classPath,String methodName,Map&String,String& args1,Map&String,Object& args2) {
Class&?& simpleClass = Class.forName(classPath);
Method simpleMethod =
Object simpelObject = simpleClass.newInstance();
simpleMethod = simpleClass.getDeclaredMethod(methodName, Map.class,Map.class);
simpleMethod.setAccessible(true);
Map&String,String& result = (Map&String, String&) simpleMethod.invoke(simpelObject, args1,args2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
public static void main(String[] args)
Map map1 = new HashMap();
map1.put("aa", "kill me!");
Map map2 = new HashMap();
map2.put("bb", " don't kill me !");
sendMsg("mon.ReflectInstance","sendMsg",map1,map2);
public Map&String,String& sendMsg(Map&String,String& args,Map&String,Object& args2){
System.out.println("order-----------"+args);
Map&String,String& map = new HashMap&String,String&();
map.put("test", args.get("aa"));
浏览: 11946 次
来自: 深圳8685人阅读
c++专区(281)
大话设计模式(书)(1)
MFC客户端(143)
--------------------------------&代码优化之避免使用过多ifelse&---------------------------------
在q.com/cn网站上看了一本书叫《ThoughtWorks文集》,里边有一章讲的是“对象健身操”,其中提到了“拒绝使用else关键字”。那么如何“拒绝使用else关键字”呢?
& & & &1、如果程序中只有一个else,如下:
& & & & & & if(con){
& & & & & & & & & dosomething();
& & & & & & }else{
& & & & & & & & & dootherthings();
& & & & & & }
& & & & 可以如下拒绝else:
& & & & & & if(con){
& & & & & & & & & dosomething();
& & & & & & & & &
& & & & & & }
& & & & & & dootherthings();
& & & & &或者使用三元运算符:con ? dosometing() : dootherthings();
& & & & 2、如果程序中有多个else,如下:
& & & & & & if(con1){
& & & & & & & & & dothing1();
& & & & & & }else if(con2){
& & & & & & & & & dothing2();
& & & & & & }else if(con3){
& & & & & & & & & dothing3();
& & & & & & }......
& & & & 可是使用策略模式或多态机制来拒绝else(还有很多其他方式),下面先介绍“策略模式”的方式:
& & & & 首先讲一个使用if...else...的例子:
& & & & package ifelse.
& & & & public class UseIfElse {
& & & & & & public static void main(String args[]){
& & & & & & & & MyPaper myPaper = new MyPaper(PaperColor.RED);
& & & & & & & & if(myPaper.getMyPaperColor() == PaperColor.BLACK){
& & & & & & & & & & System.out.println(&You need a black pen!&);
& & & & & & & & }else if(myPaper.getMyPaperColor() == PaperColor.BLUE){
& & & & & & & & & & System.out.println(&You need a blue pen!&);
& & & & & & & & }else if(myPaper.getMyPaperColor() == PaperColor.RED){
& & & & & & & & & & System.out.println(&You need a red pen!&);
& & & & & & & & }else if(myPaper.getMyPaperColor() == PaperColor.WHITE){
& & & & & & & & & & System.out.println(&You need a white pen!&);
& & & & & & & & }
& & & & & &}
& & & &class MyPaper{
& & & & & &private PaperColor paperC
& & & & & &public MyPaper(PaperColor paperColor){
& & & & & & & &this.paperColor = paperC
& & & & & &}
& & & & & &public PaperColor getMyPaperColor(){
& & & & & & & &return this.paperC
& & & & & &}
& & & &enum PaperColor{
& & & & & &WHITE, BLACK, BLUE, RED
& & & &使用if...else...的弊端在于:不利于对程序的扩展,如果新添加了一个颜色类型,那么就得去修改程序再添加一个if...else...分支,根据“开-闭原则”的宗旨:对扩展开,对修改闭。显然是用if...else...已经go out了。
& & & &下面讲一个使用“策略模式”解决上述问题的办法:
& & & &package ifelse.
& & & &public class NoIfElse {
& & & & & &public static void main(String args[]){
& & & & & & & &MyPaper myPaper = new MyPaper(new White());
& & & & & & & &myPaper.choicePen();
& & & & & }
& & & &interface PaperColor{
& & & & & &public void getPenColor();
& & & &class White implements PaperColor{
& & & & & &public void getPenColor(){
& & & & & & & &System.out.println(&You need a white pen!&);
& & & & & &}
& & & &class Red implements PaperColor{
& & & & & &public void getPenColor(){
& & & & & & & &System.out.println(&You need a red pen!&);
& & & & & &}
& & & &class Blue implements PaperColor{
& & & & & &public void getPenColor(){
& & & & & & & &System.out.println(&You need a blue pen!&);
& & & & & &}
& & & &class Black implements PaperColor{
& & & & & &public void getPenColor(){
& & & & & & & &System.out.println(&You need a black pen!&);
& & & & & &}
& & & &class MyPaper{
& & & & & &private PaperColor paperC
& & & & & &public MyPaper(PaperColor paperColor){
& & & & & & & &this.paperColor = paperC
& & & & & &}
& & & & & &public PaperColor getPaperColor(){
& & & & & & & &return this.paperC
& & & & & &}
& & & & & &public void choicePen(){
& & & & & & & &this.paperColor.getPenColor();
& & & & & &}
& & &if...else...的每个分支语句都做一件事,因此我们可以把这么些事单独封装在一个类中,就有了White、Blue、Red、Black4个类,这四个类都实现了接口PaperColor,我们再定义一个MyPaper的类(此类具有类似负载均衡的作用,分按照不同的请求分别调用跟前面4个类中的一个)。这样就把if...else...屏蔽掉了。
& & 接下来介绍使用“多态”机制来实现拒绝else:
& & package ifelse.no.P
& & public class NoIfElse_Polymorphism {
& & & & public static void main(String args[]){
& & & & & & MyPaper myPaper = new MyPaper();
& & & & & & myPaper.choice(new Black());
& & class White {
& & & & public void getPenColor(){
& & & & & & System.out.println(&You need a white pen!&);
& & class Red {
& & & & public void getPenColor(){
& & & & & & System.out.println(&You need a red pen!&);
& & class Blue {
& & & & public void getPenColor(){
& & & & & & System.out.println(&You need a blue pen!&);
& & class Black {
& & & & public void getPenColor(){
& & & & & & System.out.println(&You need a black pen!&);
& & class MyPaper{
& & & & public void choice(White white){
& & & & & & white.getPenColor();
& & & & public void choice(Red red){
& & & & & & red.getPenColor();
& & & & public void choice(Blue blue){
& & & & & & blue.getPenColor();
& & & & public void choice(Black black){
& & & & & & black.getPenColor();
& & & 此处使用的多态机制指:方法的重构,根据方法名相同儿参数不同的机制,来实现拒绝关键字。
注意:上述两种方式适用于同时只有一个分支被执行的情况。
参考:“java设计模式”之“策略模式”。
=================================&代码优化之避免使用过多ifelse&======================================
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1426784次
积分:21640
积分:21640
排名:第260名
原创:728篇
转载:18篇
评论:257条
(1)(1)(13)(18)(9)(14)(13)(5)(22)(18)(4)(8)(22)(36)(5)(29)(23)(13)(17)(36)(36)(13)(51)(5)(1)(9)(51)(28)(74)(17)(46)(48)(23)(41)}

我要回帖

更多关于 r语言 ifelse 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信