Java Builder和静态工厂方法用法总结(Effective java)

1 Java Builder模式         

       我们在创建类的时候正常的就是定义几个属性,然后提供get,set方法,再加几个构造函数,如果对象的构造函数有多个,可能在实例化的时候会出问题,代码也不够优雅,这是我们通常会用lombok的注解,比如@Builder,@Data,@NoArgsConstructor,@AllArgsConstructor等等,如果我们不用注解的话,也可以自己写,下面的写法也是Effective java书中的推荐的写法,希望在以后写代码的过程中多借鉴这种优雅的写法和思路,示例如下

public class Effective2 {
    private String requestId;
    private String name;
    private Integer age;

    public Effective2(String requestId, String name, Integer age) {
        this.requestId = requestId;
        this.name = name;
        this.age = age;
    }


    public static Effective2.Effective2Builder builder() {
        return new Effective2.Effective2Builder();
    }

    public static class Effective2Builder{
        private String requestId;
        private String name;
        private Integer age;
        public Effective2Builder() {
        }

        public Effective2.Effective2Builder requestId(String requestId) {
            this.requestId = requestId;
            return this;
        }
        public Effective2.Effective2Builder name(String name) {
            this.name = name;
            return this;
        }

        public Effective2.Effective2Builder age(Integer age) {
            this.age = age;
            return this;
        }
        public Effective2 build() {
            return new Effective2(this.requestId, this.name, this.age);
        }
    }


    public static void main(String[] args) {
        Effective2 effective2 =Effective2.builder().requestId("111").name("java").age(11).build();
        System.out.println(effective2.name);
    }
}

2 静态工厂方法 

       有时候需要new一个对象,直接通过new就出来了,这是其实有一个最佳实践就是运用静态工厂方法来获取对象,好处是我们可以更直观的看到此对象的创建的特征,还有就是不必在每次创建的时候都创建个新对象,这使得不可变类可以预先创建好实例(作为缓存重复使用,有助于控制类的实例),比如Boolean.valueOf(boolean),下面通过一个简单的demo来看下,这个demo只是展示用的,实际代码中通常用接口来代替map返回值的

public class DefaultResultFactory {

    private static final DefaultResultInfo defaultResult =new DefaultResultInfo();

    private DefaultResultFactory(){

    }

    public static Map<String, String> getResultForMap(){
        return defaultResult.getResultForMap();
    }

    public static Map<String, List<String>> getResultForMapList(){
        return defaultResult.getResultForMapList();
    }

    public static Map<String, Set<String>> getResultForMapSet(){
        return defaultResult.getResultForMapSet();
    }

}

public class DefaultResultInfo {

    private Map<String, String> resultForMap=ResultBuilder.newInstance();

    private Map<String, List<String>> resultForList= ResultBuilder.newInstance();

    private Map<String, Set<String>> resultForSet=ResultBuilder.newInstance();


    public Map<String, String> getResultForMap(){
        return resultForMap;
    }

    public Map<String, List<String>> getResultForMapList(){
        return resultForList;
    }

    public Map<String, Set<String>> getResultForMapSet(){
        return resultForSet;
    }
 }

public class ResultBuilder {

    /**
     * 定义静态方法
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> HashMap<K, V> newInstance() {
        return new HashMap<K, V>();
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值