search

Home  >  Q&A  >  body text

if - Java代码多分支语句优化

1.项目背景
一个controller,一个serivce,多个dao层
service里面更具用户的不同参数调用不同的dao进行实例化,时候在调用方法。
2.不要问为什么设计,那是因为一个模块对应37张表,用户根据下拉选择输入不同的表单(说白点,点击页面新增弹出下拉框,让用户选择填写那张表单,再来一句,各个表之间没有相同的字段),所以此模块肯定是一个controller,一个service,各个dao对应不同的表进行增删改查操作
3.所以在service层必然有一个语句需要判断实例化那个dao,用注入也行
if(flag == dao1){

}else if(flag == dao2){

}else if(flag == dao3){

}else if(flag == dao4){

}else if(flag == dao5){

}else if(flag == dao6){

}
........就要写25个(不管是switch还是if都避免不了)
4.所以这样的方式有没有好一点的方式进行处理,不用写这么多的判断。

PHP中文网PHP中文网2827 days ago565

reply all(8)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:41:01

    I think it would be good to call the Dao layer through reflection without repeating the name of dao from the front end;
    Just a thought.

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:41:01

    Since your question already contains several affirmative sentences such as "don't ask" and "definitely", it's hard to say anything else. You can try factory mode or manager mode to get the corresponding dao instance based on the flag value (actually, you just move this ugly code to a dark corner).

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:41:01

    Polymorphism, or table-driven mode (refer to Chapter 18 of the Code Collection)

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:41:01

    Obviously it’s a design problem, it’s still a bit stubborn

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:41:01

    Project structure problem. Think about the characteristics of java. Encapsulation. Inheritance polymorphism

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:41:01

    Since you mentioned the word injection, I assume that you are using the Spring framework, and Spring's applicationContext can get all the beans that have been initialized by the container. Usually there is a data access layer abstraction such as BaseDao in the framework. Then you can use baseDao=applicationContext.getBean("beanName"), and then you can do whatever you want

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:41:01

    I will build a map
    map.put("dao1",dao1);
    map.put("dao2",dao2);
    map.put("dao3",dao3);
    Then select the map through the parameters of the page Key, get dao
    Of course, if you are using spring, this map can be directly configured in spring xml

    reply
    0
  • PHPz

    PHPz2017-04-17 17:41:01

    I remember that both if else and switch case statements can be reconstructed using command mode

    interface dao
    {
        crdu() ;
    }
    
    class ConcreteDao implements dao
    {
        crdu(){...} ;
    }
    
    class Service
    {
        private Dao dao ;
    
        public void set(Dao dao){}
    
        public void service(){dao.service();}
    }
    
    class ServiceControl
    {
        private HashMap<Integer , Dao> map ;
        private Service service ;
    
        public ServiceControl()
        {
            //读xml表初始化标识与dao的对应
        }
    
        public void service(int flag)
        {
            //根据flag来选择不同的dao注入到service中
            Dao dao = map.get(flag) ;
            service.set(dao) ;
            service.service() ;
        }
    }
    

    Then the control layer can directly use servicecontrol, so that the control layer is decoupled from the dao, and the modifications on both sides do not affect each other and are coordinated through xml

    reply
    0
  • Cancelreply