构造者模式
可以解决什么问题
可以一定程度上保证原子性,原理是在中间加了一层,相比之前一个一个set可以防止在set过程中被其他线程拿去使用,但是这时候它并不是完整的所有会存在问题。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package io.quarkiverse.seata.design;
import cn.hutool.core.collection.CollectionUtil;
import java.util.*; import java.util.stream.Collectors;
public class ChainNode{ private List<Tuple<Object,Object>> params;
ChainNode(List<Tuple<Object,Object>> params){
this.params = params; }
public List<Tuple<Object,Object>> getParams(){ return this.params; }
public static ChainNode.ChainNodeBuilder builder(){ return new ChainNode.ChainNodeBuilder(); } public static class ChainNodeBuilder{ private List<Tuple<Object,Object>> params;
public ChainNode.ChainNodeBuilder params(Tuple<Object,Object>... params){ return params != null && params.length>0 ? this.params( (Arrays.stream(params)· .collect(Collectors.toList()) ) ):this; }
public ChainNode.ChainNodeBuilder params(Collection<Tuple<Object,Object>> params){ if (params != null && params.size()>0){ this.params = params.stream().filter(Objects::nonNull).collect(Collectors.toList()); } return this; } ChainNodeBuilder(){}
public ChainNode build(){ return new ChainNode(this.params); } } }
|