String TYPE = methodA();         
     if("A".equals(TPYE))
        {
TEXT_VARIABLE = ("'S.Country."+TEXT_VARIABLE+"'");
}
if("B".equals(TPYE))
{
TEXT_VARIABLE = ("'S.Region."+TEXT_VARIABLE+"'");
}
          if("C".equals(TPYE))
{
TEXT_VARIABLE = ("'S.Country."+TEXT_VARIABLE+"'");
}
.................如果我有几十个if就只能这样排列下去吗?因为TPYE是String不能用switch.大侠们有什么好办法重构一下这里吗?模板模式?拜托了

解决方案 »

  1.   

    if
    else if
    else if
    else if 
    else
      

  2.   

    直接把比对用的全部信息存放到map中,key就用TPYE  取的时候直接get
      

  3.   

    可以写成两个数组 一个循环即可 A,B,C……,即可一个数组。("'S.Country."+TEXT_VARIABLE+"'……这些值写成一个数组 用一个for循环一一对应即可
      

  4.   

    采用策略模式解决这个问题
    一个if抽成一个类..写一个接口,这个接口提供一个方法所有类(if抽成的类)实现这个接口
    这个时候采用策略模式的好处就是
    当你再新一个类时,不需要改以前的代码,,只需要写个类,实现接口。
    如果不采用策略模式的话你就需要在原来的基础上,又加一个if
    哪天你需求要改变,,,要去掉其中的一个,,你又得把其中的一个if去掉
      

  5.   

    策略不错,也可以吧这些String写为枚举,然后switch
      

  6.   

    用枚举吧,就可以实现switch。不过用策略模式最好
      

  7.   

    用 jdk 1.7 吧 支持switch 用String做条件了
      

  8.   

    Using JDK 1.7 switch statements:The switch construct now is being used with bytes & primitive types, but with JDK 1.7 switch can be used with String / Objects as shown below:-String animal = “dog”;switch(animal){case “dog” : takeForWalk(animal);case “cat” : feedMilk(animal);}
      

  9.   

    用switch去替换,感觉。加油,楼 主。
      

  10.   


    LZ可以用 状态模式 ,一个if对应一种状态,具体的内容还得LZ自学哦!
      

  11.   

    设计思想:使用map<暂定为testMap>存储映射关系,使用不同的Type 作为key,它对应的属性作为value形如:
    A:Country.
    B:Region
    这样的话TEXT_VARIABLE 的赋值语句可以写成:
    String str = testMap.get(Type);
    if(!"".equals(str) && null!=str){
        TEXT_VARIABLE = "'S." + str + TEXT_VARIABLE + "'";
    }
    大概就是这个意思,具体LZ在测试下吧
      

  12.   

    import java.util.HashMap;
    import java.util.Map;
    public class IfTest {
    public static void main(String[] args) {
    String type = "C";
    String TEXT_VARIABLE = ":天津";
    Map testMap  = new HashMap();
    testMap.put("A", "Country");
    testMap.put("B", "Region");
    testMap.put("C","Province");
    testMap.put("D", "City");

    String str = (String)testMap.get(type);
    if(!"".equals(str) && null!=str){
      TEXT_VARIABLE = "'S." + str + TEXT_VARIABLE + "'";
    }
    System.out.println("TEXT_VARIABLE = " + TEXT_VARIABLE);
    }}
      

  13.   


    不会设计模式学设计模式,会点设计模式滥用设计模式麻烦这位大侠 batey520 用策略模式思想写写?//本人猜想 用这位所说的策略模式来实现//***********context.java*************
    public class Context {
        private Strategy strategy;
        
        public Context(Strategy strategy) {
            this.strategy = strategy;
        }    public void contextRequest(String s){
         strategy.doEquals(s);
        }
    }
    //***************Strategy.java****************
    //抽象Equals的接口
    public interface Strategy {    void doEquals(String s);
        
    }
    //***************ConcreteStrategy_*.java******************
    //所以if抽像成的类
    public class ConcreteStrategy_Region implements Strategy {
        String TYPE = methodA();
        //定义一下 TEXT_VARIABLE,虽然我没看懂LZ的这个到底是什么的东西,暂且引用一下吧
        public void doEquals(String s) {
    if(s.equals(TYPE)){TEXT_VARIABLE = ("'S.Region."+TEXT_VARIABLE+"'");}
    }
    }
    public class ConcreteStrategy_Country implements Strategy {
        String TYPE = methodA();
        //定义一下 TEXT_VARIABLE
        public void doEquals(String s) {
    if(s.equals(TYPE)){TEXT_VARIABLE =("'S.Country."+TEXT_VARIABLE+"'");}
    }
    }
    //*****************StrategyTest.java********************
    public class StrategyTest {    /**
         * @param args
         */
        public static void main(String[] args) {
            Strategy strategy_Region = new ConcreteStrategy_Region();
            Context context_Region = new Context(strategy_Region);
            
            Strategy strategy_Country = new ConcreteStrategy_Country();
            Context context_Country = new Context(strategy_Country );//N多String,我这里只看3个
            String s1 = "A";
            String s2 = "B";
            String s3 = "C";        context.contextRequest_Country(s1);
            context.contextRequest_Region(s2);
            context.contextRequest_Country(s3);
        }}
    小弟不才,只能这样实现 策略模式 思想,你看看这个在效率上,在可读性上有LZ的 全if 好吗?重新声明这句话:不会设计模式学设计模式,会点设计模式滥用设计模式寻CSDN老鸟点评。。