Builder模式如何在Java中实现-创新互联
这篇文章给大家介绍Builder模式如何在Java中实现,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
10年积累的成都做网站、网站制作、成都外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有阿拉山口免费网站建设让你可以放心的选择与我们合作。Pre Java8
我们先来看下在Java8之前的Builder模式
public class Order { private String code; private Listoffers; private Map features; public static Order.Builder builder(){ return new Builder(); } //省略getter setter public static class Builder { private OrderState orderState = new OrderState(); private static final BeanCopier orderCopier = BeanCopier.create(OrderState.class, Order1.class, false); private class OrderState { private String code; private Map features; private List offers; //省略getter setter } public Builder code(String code) { orderState.code = code; return this; } public Builder features(Map features) { orderState.features = features; return this; } public Builder feature(String key, T obj) { if (orderState.features == null) { orderState.features = new HashMap<>(); } orderState.features.put(key, obj); return this; } public Builder offers(List offers) { orderState.offers = offers; return this; } public Builder offer(String offer) { if (orderState.offers == null) { orderState.offers = new ArrayList<>(); } orderState.offers.add(offer); return this; } public Order build() { Order order = new Order(); orderCopier.copy(orderState, order1, null); orderState = null; return order; } } }
新闻标题:Builder模式如何在Java中实现-创新互联
网站网址:http://scyanting.com/article/eopdd.html