Java方法提取成公共方法复用
PHP中文网
PHP中文网 2017-04-18 09:40:32
0
1
359
public class A { //是否使用缓存 private boolean flag = true; public Object method(String param1, String param2) {//参数不固定 Object o; if(flag) { B b = new B();//相当于缓存 o = b.get("o"); if(o == null) { o = C.get(param1, param2);//相当于从数据库里查数据 b.set("o", o);//在放入缓存 } } else { o = C.get(param1, param2);//直接查询 } return o; } }

大致意思就是把缓存做成可控制的,用一个 boolean 值来控制开关,程序里要做很多判断,请教有办法把这逻辑整理成一个公共的方法吗?每次调用一下公共方法传写参数即可,是否开启缓存都在公共方法里处理!

PHP中文网
PHP中文网

认证0级讲师

reply all (1)
Peter_Zhu

This problem can be solved using decoration mode. This is how mybatis handles it (you can look at the relationship between CachingExecutor and Executor).

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!