建造者设计模式
将一个复杂对象的构建和它的表示分离,使得同样的构建过程,可以创建不同的表示
流行实践
Joshua Bloch改进的建造者模式(主流方式)
下面这个实现了建造者模式的类,注意以下6点:
1,它里面的静态内部类MyCacheBuilder才是神来之笔。(主类是产品,静态内部类是工厂,工厂生产产品)
2,它的构造函数是private的(有点像单例模式,防止用户绕开工厂,自己直接建立对象)
3,它的参数,没有提供set方法(主类(产品)是只读的,绝对的线程安全)
4,它里面必须(不能为空)的参数,添加了final关键字
5,静态内部类MyCacheBuilder的set方法,不同于常规的set方法,注意它的返回值
6,静态内部类MyCacheBuilder的build方法,非常重要。
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| public class MyCache<K, V> {
private final int initialCapacity;
private final long maximumSize;
private int concurrencyLevel = -1;
private long maximumWeight = -1L;
private long expireAfterWriteNanos = -1L;
private long expireAfterAccessNanos = -1L; private MyCache(MyCacheBuilder myCacheBuilder) { this.initialCapacity = myCacheBuilder.initialCapacity; this.maximumSize = myCacheBuilder.maximumSize; this.concurrencyLevel = myCacheBuilder.concurrencyLevel; this.maximumWeight = myCacheBuilder.maximumWeight; this.expireAfterWriteNanos = myCacheBuilder.expireAfterWriteNanos; this.expireAfterAccessNanos = myCacheBuilder.expireAfterAccessNanos; }
@Override public String toString() { return "MyCache{" + "initialCapacity=" + initialCapacity + ", maximumSize=" + maximumSize + ", concurrencyLevel=" + concurrencyLevel + ", maximumWeight=" + maximumWeight + ", expireAfterWriteNanos=" + expireAfterWriteNanos + ", expireAfterAccessNanos=" + expireAfterAccessNanos + '}'; }
public void put(K key, V value) { }
public V get(K key) { return null; }
public static class MyCacheBuilder<K, V> {
private final int initialCapacity;
private final long maximumSize; private final Map<String, String> cacheMap = null;
private int concurrencyLevel = -1;
private long maximumWeight = -1L;
private long expireAfterWriteNanos = -1L;
private long expireAfterAccessNanos = -1L;
public MyCacheBuilder(int initialCapacity, long maximumSize) { this.initialCapacity = initialCapacity; this.maximumSize = maximumSize; }
public MyCacheBuilder setConcurrencyLevel(int concurrencyLevel) { this.concurrencyLevel = concurrencyLevel; return this; }
public MyCacheBuilder setMaximumWeight(long maximumWeight) { this.maximumWeight = maximumWeight; return this; }
public MyCacheBuilder setExpireAfterWriteNanos(long expireAfterWriteNanos) { this.expireAfterWriteNanos = expireAfterWriteNanos; return this; }
public MyCacheBuilder setExpireAfterAccessNanos(long expireAfterAccessNanos) { this.expireAfterAccessNanos = expireAfterAccessNanos; return this; }
public MyCache build() { return new MyCache<K, V>(this); } } }
|
源码中的建造者模式
查询条件:
repo:^github.com/openjdk/jdk$ file:^src/java.base -file:test lang:Java file:builder.java count:all
StringBuilder类
Calendar类
repo:^github.com/spring-projects/spring-framework$ -file:test lang:Java file:builder.java count:all
UriComponentsBuilder
致谢
1.https://www.bilibili.com/read/cv17885668?spm_id_from=333.999.0.0