我所知道设计模式之建造者模式
前言需求
接下里介绍的是Java 的设计模式之一:建造者模式
需要建房子:这一过程为打桩、砌墙、封顶
房子有各种各样的,比如普通房,高楼,别墅
各种房子的过程虽然一样,但是要求不要相同的
请问你会怎么制作呢?
一、什么是建造者模式
建造者模式(Builder Pattern):又叫生成器模式,是一种对象构建模式
。
它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象
。
建造者模式是一步一步创建一个复杂的对象
,它允许用户只通过指定复杂对象的类型和内容就可以构建
它们, 用户不需要知道内部的具体构建细节
文章图片
二、通过示例说明情况
【我所知道设计模式之建造者模式】我们使用传统的方式解决盖房子问题,一般抽象一个父类出来创建方式
文章图片
abstract class AbstractHouse {
//打地基
public abstract void buildBasic();
//砌墙
public abstract void buildWalls();
//封顶
public abstract void roofed();
//按照顺序建房子
public void build() { buildBasic();
buildWalls();
roofed();
}
}
假如我们是普通的房子就是继承这个父类
class CommonHouse extends AbstractHouse {
@Override
public void buildBasic() {
System.out.println(" 普通房子打地基 ");
}
@Override
public void buildWalls() {
System.out.println(" 普通房子砌墙 ");
}
@Override
public void roofed() {
System.out.println(" 普通房子封顶 ");
}
}
当我们创建普通房子的时候,即可直接调用普通房子的实现类即可
public static void main(String[] args) {
CommonHouse commonHouse = new CommonHouse();
commonHouse.build();
}运行结果如下:
普通房子打地基
普通房子砌墙
普通房子封顶
传统方式的问题分析
- 优点是
比较好理解,简单易操作
。 - 缺点是
设计的程序结构,过于简单,没有设计缓存层对象,程序的扩展和维护不好
.
把产品(即:房子) 和 创建产品的过程(即:建房子流程) 封装在一起,耦合性增强
了。解决方案:
将产品和产品建造过程解耦
=> 建造者模式三、建造者模式的四个角色
- Product(产品角色):
一个具体的产品对象
。 - Builder(抽象建造者):
创建一个 Product 对象的各个部件指定的 接口/抽象类
。 - ConcreteBuilder(具体建造者):
实现接口,构建和装配各个部件
。 - Director(指挥者):
构建一个使用 Builder 接口的对象
。
它主要有两个作用:
1:隔离了客户与对象的生产过程
2:负责控制产品对象的生产过程
文章图片
比如说这辆车是一个产品,我们只需要将方向盘、轮胎、发动机进行组装
四、建造者模式原理类图
文章图片
将对象构造代码从产品类中抽取出来, 并将其放在为生成器的独立对象中
文章图片
五、建造者模式解决盖房子问题
使用建造者模式进行优化,我们先根据四个进行类图画一画
文章图片
现在我们创建一个产品:房子
// 产 品 ->Product
class House {
private String baise;
//地基
private String wall;
//墙
private String roofed;
//屋顶
public String getBaise() {
return baise;
}
public void setBaise(String baise) {
this.baise = baise;
}
public String getWall() {
return wall;
}
public void setWall(String wall) {
this.wall = wall;
}
public String getRoofed() {
return roofed;
}
public void setRoofed(String roofed) {
this.roofed = roofed;
}
}
接着我们需要一个建造者,提供建造的流程、组合产品所需方法
// 抽象的建造者
abstract class HouseBuilder {
protected House house = new House();
//将建造的流程所需写好, 抽象的方法
public abstract void buildBasic();
public abstract void buildWalls();
public abstract void roofed();
//建造房子好, 将产品(房子) 返回
public House buildHouse() {return house;
}
}
接下里我们进行普通房子的具体建造者实现
class CommonHouse extends HouseBuilder {
@Override
public void buildBasic() {
System.out.println(" 普通房子打地基 5 米 ");
}
@Override
public void buildWalls() {
System.out.println(" 普通房子砌墙 10cm ");
}
@Override
public void roofed() {
System.out.println(" 普通房子屋顶 ");
}
}
至于具体的建造流程,是先砌墙、先弄地基、还是屋顶交给指挥者来决定
//指挥者,这里去指定制作流程,返回产品
class HouseDirector {HouseBuilder houseBuilder = null;
//构造器传入 houseBuilder
public HouseDirector(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}//通过 setter 传入 houseBuilder
public void setHouseBuilder(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}//如何处理建造房子的流程,交给指挥者
public House constructHouse() {
houseBuilder.buildBasic();
houseBuilder.buildWalls();
houseBuilder.roofed();
return houseBuilder.buildHouse();
}
}
接下里一起使用demo 来体会一下建造者模式下盖普通房、盖高楼有何区别
public static void main(String[] args) {//盖普通房子
CommonHouse commonHouse = new CommonHouse();
//准备创建房子的指挥者
HouseDirector houseDirector = new HouseDirector(commonHouse);
//完成盖房子,返回产品(普通房子)
House house = houseDirector.constructHouse();
//System.out.println(" 输 出 流 程 ");
System.out.println("--------------------------");
}运行结果:
普通房子打地基 5 米
普通房子砌墙 10cm
普通房子屋顶
--------------------------
那么这时我们要盖高楼呢?盖别墅呢?
public static void main(String[] args) {//盖高楼
HighBuilding highBuilding = new HighBuilding();
//准备创建房子的指挥者
HouseDirector houseDirector = new HouseDirector(highBuilding);
//完成盖房子,返回产品(高楼)
House house = houseDirector.constructHouse();
//System.out.println(" 输 出 流 程 ");
System.out.println("--------------------------");
}
运行结果:
高楼的打地基 100 米
高楼的砌墙 20cm
高楼的透明屋顶
相比之前的抽取父类,普通房子与高楼会有一些不同的建造细节
而抽取父类在砌墙、盖屋顶、打地基还可以,若高楼一些别的需求与父类是没有的,那么就可能又要抽取一个高楼的父类
建造者模式呢,只需建造者里提供建造的流程、组合、细节的方法
六、建造者模式在 JDK 的应用和源码分析
我们一起来看看java.lang.StringBuilder中的建造者模式
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("hello,world");
System.out.println(stringBuilder);
}运行结果:
hello,world
那么我们一起来看看StringBuilder 这个类是怎么样的呢?
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{static final long serialVersionUID = 4383685877147921099L;
public StringBuilder() {
super(16);
}public StringBuilder(int capacity) {
super(capacity);
}public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}@Override
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}@Override
public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}@Override
public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}@Override
public StringBuilder append(char[] str) {
super.append(str);
return this;
}@Override
public StringBuilder append(char[] str, int offset, int len) {
super.append(str, offset, len);
return this;
}@Override
public StringBuilder append(boolean b) {
super.append(b);
return this;
}@Override
public StringBuilder append(char c) {
super.append(c);
return this;
}@Override
public StringBuilder append(int i) {
super.append(i);
return this;
}@Override
public StringBuilder append(long lng) {
super.append(lng);
return this;
}@Override
public StringBuilder append(float f) {
super.append(f);
return this;
}@Override
public StringBuilder append(double d) {
super.append(d);
return this;
}@Override
public StringBuilder appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
}@Override
public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}@Override
public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}@Override
public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}@Override
public StringBuilder insert(int index, char[] str, int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}
@Override
public StringBuilder insert(int offset, Object obj) {
super.insert(offset, obj);
return this;
}
@Override
public StringBuilder insert(int offset, String str) {
super.insert(offset, str);
return this;
}@Override
public StringBuilder insert(int offset, char[] str) {
super.insert(offset, str);
return this;
}@Override
public StringBuilder insert(int dstOffset, CharSequence s) {
super.insert(dstOffset, s);
return this;
}@Override
public StringBuilder insert(int dstOffset, CharSequence s,
int start, int end)
{
super.insert(dstOffset, s, start, end);
return this;
}@Override
public StringBuilder insert(int offset, boolean b) {
super.insert(offset, b);
return this;
}@Override
public StringBuilder insert(int offset, char c) {
super.insert(offset, c);
return this;
}
@Override
public StringBuilder insert(int offset, int i) {
super.insert(offset, i);
return this;
}
@Override
public StringBuilder insert(int offset, long l) {
super.insert(offset, l);
return this;
}@Override
public StringBuilder insert(int offset, float f) {
super.insert(offset, f);
return this;
}@Override
public StringBuilder insert(int offset, double d) {
super.insert(offset, d);
return this;
}@Override
public int indexOf(String str) {
return super.indexOf(str);
}@Override
public int indexOf(String str, int fromIndex) {
return super.indexOf(str, fromIndex);
}@Override
public int lastIndexOf(String str) {
return super.lastIndexOf(str);
}@Override
public int lastIndexOf(String str, int fromIndex) {
return super.lastIndexOf(str, fromIndex);
}@Override
public StringBuilder reverse() {
super.reverse();
return this;
}@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
s.defaultWriteObject();
s.writeInt(count);
s.writeObject(value);
}private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
value = https://www.it610.com/article/(char[]) s.readObject();
}}
我们发现这个类是final类并继承了AbstractStringBuilder,进去父类看看
abstract class AbstractStringBuilder implements Appendable, CharSequence {char[] value;
int count;
AbstractStringBuilder() {}AbstractStringBuilder(int capacity) {
value = https://www.it610.com/article/new char[capacity];
}@Override
public int length() {
return count;
}public int capacity() {
return value.length;
}
public void ensureCapacity(int minimumCapacity) {
if (minimumCapacity> 0)
ensureCapacityInternal(minimumCapacity);
}private void ensureCapacityInternal(int minimumCapacity) {
if (minimumCapacity - value.length > 0) {
value = https://www.it610.com/article/Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private int newCapacity(int minCapacity) {
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
? hugeCapacity(minCapacity)
: newCapacity;
}private int hugeCapacity(int minCapacity) {
if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
throw new OutOfMemoryError();
}
return (minCapacity> MAX_ARRAY_SIZE)
? minCapacity : MAX_ARRAY_SIZE;
}public void trimToSize() {
if (count < value.length) {
value = https://www.it610.com/article/Arrays.copyOf(value, count);
}
}public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
ensureCapacityInternal(newLength);
if (count < newLength) {
Arrays.fill(value, count, newLength,'\0');
}count = newLength;
}
@Override
public char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}public int codePointAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, count);
}public int codePointBefore(int index) {
int i = index - 1;
if ((i < 0) || (i >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointBeforeImpl(value, index, 0);
}public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
}public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > count) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, count,
index, codePointOffset);
}
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
{
if (srcBegin < 0)
throw new StringIndexOutOfBoundsException(srcBegin);
if ((srcEnd < 0) || (srcEnd > count))
throw new StringIndexOutOfBoundsException(srcEnd);
if (srcBegin > srcEnd)
throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
public void setCharAt(int index, char ch) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
value[index] = ch;
}
public AbstractStringBuilder append(Object obj) {
return append(String.valueOf(obj));
}public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return appendNull();
int len = sb.length();
ensureCapacityInternal(count + len);
sb.getChars(0, len, value, count);
count += len;
return this;
}AbstractStringBuilder append(AbstractStringBuilder asb) {
if (asb == null)
return appendNull();
int len = asb.length();
ensureCapacityInternal(count + len);
asb.getChars(0, len, value, count);
count += len;
return this;
}@Override
public AbstractStringBuilder append(CharSequence s) {
if (s == null)
return appendNull();
if (s instanceof String)
return this.append((String)s);
if (s instanceof AbstractStringBuilder)
return this.append((AbstractStringBuilder)s);
return this.append(s, 0, s.length());
}private AbstractStringBuilder appendNull() {
int c = count;
ensureCapacityInternal(c + 4);
final char[] value = https://www.it610.com/article/this.value;
value[c++] ='n';
value[c++] = 'u';
value[c++] = 'l';
value[c++] = 'l';
count = c;
return this;
}@Override
public AbstractStringBuilder append(CharSequence s, int start, int end) {
if (s == null)
s = "null";
if ((start < 0) || (start > end) || (end > s.length()))
throw new IndexOutOfBoundsException(
"start " + start + ", end " + end + ", s.length() "
+ s.length());
int len = end - start;
ensureCapacityInternal(count + len);
for (int i = start, j = count;
i < end;
i++, j++)
value[j] = s.charAt(i);
count += len;
return this;
}
public AbstractStringBuilder append(char[] str) {
int len = str.length;
ensureCapacityInternal(count + len);
System.arraycopy(str, 0, value, count, len);
count += len;
return this;
}public AbstractStringBuilder append(char str[], int offset, int len) {
if (len > 0)// let arraycopy report AIOOBE for len < 0
ensureCapacityInternal(count + len);
System.arraycopy(str, offset, value, count, len);
count += len;
return this;
}public AbstractStringBuilder append(boolean b) {
if (b) {
ensureCapacityInternal(count + 4);
value[count++] = 't';
value[count++] = 'r';
value[count++] = 'u';
value[count++] = 'e';
} else {
ensureCapacityInternal(count + 5);
value[count++] = 'f';
value[count++] = 'a';
value[count++] = 'l';
value[count++] = 's';
value[count++] = 'e';
}
return this;
}@Override
public AbstractStringBuilder append(char c) {
ensureCapacityInternal(count + 1);
value[count++] = c;
return this;
}public AbstractStringBuilder append(int i) {
if (i == Integer.MIN_VALUE) {
append("-2147483648");
return this;
}
int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
: Integer.stringSize(i);
int spaceNeeded = count + appendedLength;
ensureCapacityInternal(spaceNeeded);
Integer.getChars(i, spaceNeeded, value);
count = spaceNeeded;
return this;
}public AbstractStringBuilder append(long l) {
if (l == Long.MIN_VALUE) {
append("-9223372036854775808");
return this;
}
int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
: Long.stringSize(l);
int spaceNeeded = count + appendedLength;
ensureCapacityInternal(spaceNeeded);
Long.getChars(l, spaceNeeded, value);
count = spaceNeeded;
return this;
}public AbstractStringBuilder append(float f) {
FloatingDecimal.appendTo(f,this);
return this;
}public AbstractStringBuilder append(double d) {
FloatingDecimal.appendTo(d,this);
return this;
}public AbstractStringBuilder delete(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
if (start > end)
throw new StringIndexOutOfBoundsException();
int len = end - start;
if (len > 0) {
System.arraycopy(value, start+len, value, start, count-end);
count -= len;
}
return this;
}public AbstractStringBuilder appendCodePoint(int codePoint) {
final int count = this.count;
if (Character.isBmpCodePoint(codePoint)) {
ensureCapacityInternal(count + 1);
value[count] = (char) codePoint;
this.count = count + 1;
} else if (Character.isValidCodePoint(codePoint)) {
ensureCapacityInternal(count + 2);
Character.toSurrogates(codePoint, value, count);
this.count = count + 2;
} else {
throw new IllegalArgumentException();
}
return this;
}public AbstractStringBuilder deleteCharAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
System.arraycopy(value, index+1, value, index, count-index-1);
count--;
return this;
}public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(value, start);
count = newCount;
return this;
}
public String substring(int start) {
return substring(start, count);
}@Override
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}public String substring(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
throw new StringIndexOutOfBoundsException(end);
if (start > end)
throw new StringIndexOutOfBoundsException(end - start);
return new String(value, start, end - start);
}public AbstractStringBuilder insert(int index, char[] str, int offset,
int len)
{
if ((index < 0) || (index > length()))
throw new StringIndexOutOfBoundsException(index);
if ((offset < 0) || (len < 0) || (offset > str.length - len))
throw new StringIndexOutOfBoundsException(
"offset " + offset + ", len " + len + ", str.length "
+ str.length);
ensureCapacityInternal(count + len);
System.arraycopy(value, index, value, index + len, count - index);
System.arraycopy(str, offset, value, index, len);
count += len;
return this;
}public AbstractStringBuilder insert(int offset, Object obj) {
return insert(offset, String.valueOf(obj));
}public AbstractStringBuilder insert(int offset, String str) {
if ((offset < 0) || (offset > length()))
throw new StringIndexOutOfBoundsException(offset);
if (str == null)
str = "null";
int len = str.length();
ensureCapacityInternal(count + len);
System.arraycopy(value, offset, value, offset + len, count - offset);
str.getChars(value, offset);
count += len;
return this;
}public AbstractStringBuilder insert(int offset, char[] str) {
if ((offset < 0) || (offset > length()))
throw new StringIndexOutOfBoundsException(offset);
int len = str.length;
ensureCapacityInternal(count + len);
System.arraycopy(value, offset, value, offset + len, count - offset);
System.arraycopy(str, 0, value, offset, len);
count += len;
return this;
}public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
if (s == null)
s = "null";
if (s instanceof String)
return this.insert(dstOffset, (String)s);
return this.insert(dstOffset, s, 0, s.length());
}public AbstractStringBuilder insert(int dstOffset, CharSequence s,
int start, int end) {
if (s == null)
s = "null";
if ((dstOffset < 0) || (dstOffset > this.length()))
throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
throw new IndexOutOfBoundsException(
"start " + start + ", end " + end + ", s.length() "
+ s.length());
int len = end - start;
ensureCapacityInternal(count + len);
System.arraycopy(value, dstOffset, value, dstOffset + len,
count - dstOffset);
for (int i=start;
i> 1;
j >= 0;
j--) {
int k = n - j;
char cj = value[j];
char ck = value[k];
value[j] = ck;
value[k] = cj;
if (Character.isSurrogate(cj) ||
Character.isSurrogate(ck)) {
hasSurrogates = true;
}
}
if (hasSurrogates) {
reverseAllValidSurrogatePairs();
}
return this;
}private void reverseAllValidSurrogatePairs() {
for (int i = 0;
i < count - 1;
i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
char c1 = value[i + 1];
if (Character.isHighSurrogate(c1)) {
value[i++] = c1;
value[i] = c2;
}
}
}
}@Override
public abstract String toString();
final char[] getValue() {
return value;
}}
那么这个父类他是建造者吗?其实不然,因为我们发现他已实现了接口一些方法
public interface Appendable {Appendable append(CharSequence csq) throws IOException;
Appendable append(CharSequence csq, int start, int end) throws IOException;
Appendable append(char c) throws IOException;
}
刚刚在StringBuilder构造器中填入"hello,world",就调用了append
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{static final long serialVersionUID = 4383685877147921099L;
public StringBuilder() {
super(16);
}public StringBuilder(int capacity) {
super(capacity);
}public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}}
背后调用的就是继承的父类方法,我们就能发现append其实已实现了
abstract class AbstractStringBuilder implements Appendable, CharSequence {//省略其他关键性代码.....
public AbstractStringBuilder append(Object obj) {
return append(String.valueOf(obj));
}public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return appendNull();
int len = sb.length();
ensureCapacityInternal(count + len);
sb.getChars(0, len, value, count);
count += len;
return this;
}AbstractStringBuilder append(AbstractStringBuilder asb) {
if (asb == null)
return appendNull();
int len = asb.length();
ensureCapacityInternal(count + len);
asb.getChars(0, len, value, count);
count += len;
return this;
}
}
那么怎么理解StringBuilder里的建造者模式,产品、指挥者等这些角色呢?
这里需要阐述一个观点:
我们在去查看源码的时候,有可能发现在使用的时候设计模式思想很相似,但形式上又与我们画的类图保持完全的一致
所以我们在这里没有很准确的找到产品、建造者、指挥者、具体建造者
但是建造者模式是什么呢?
建造者模式是
一步一步创建一个复杂的对象
,它允许用户只通过指定复杂对象的类型和内容就可以构建
它们, 用户不需要知道内部的具体构建细节源码中建造者模式角色分析 所以我们来分析一下StringBuilder在建造者模式下的角色
- Appendable 接口定义了多个 append 方法(抽象方法)
即 Appendable 为抽象建造者, 定义了抽象方法
- AbstractStringBuilder 实现了Appendable接口方法
这里的 AbstractStringBuilder已经是建造者,只是不能实例化
- StringBuilder 即充当了指挥者角色,同时充当了具体的建造者
建造方法的实现是由 AbstractStringBuilder 完成 , 而 StringBuilder 继承了 AbstractStringBuilder
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{//省略其他关键性代码.....
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}}
//具体建造方法由父类AbstractStringBuilder的方法实现
//建造后返回自己的类型AbstractStringBuilder
abstract class AbstractStringBuilder implements Appendable, CharSequence {//省略其他关键性代码.....public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
}
文章图片
七、建造者模式的注意事项和细节
客户端(使用程序)
不必知道产品内部组成的细节,将产品本身与产品的创建过程解耦,使得相同的创建过程可以创建不同的产品对象
每一个具体建造者都相对独立,而与其他的具体建造者无关,因此
可以很方便地替换具体建造者或增加新的具体建造者, 用户使用不同的具体建造者即可得到不同的产品对象
可以更加精细地控制产品的创建过程
。将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰, 也更方便使用程序来控制创建过程增加新的具体建造者无须修改原有类库的代码
,指挥者类针对抽象建造者类编程,系统扩展方便,符合“开闭原则
建造者模式所创建的产品一般具有较多的共同点,
其组成部分相似,如果产品之间的差异性很大,则不适合使用建造者模式
,因此其使用范围受到一定的限制。如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大
,因此在这种情况下,要考虑是否选择建造者模式参考资料
尚硅谷:设计模式(韩顺平老师):建造者模式
Refactoring.Guru:《深入设计模式》
推荐阅读
- 为什么要学习GoF设计模式()
- 你可得知道物理地址与IP地址
- 设计模式二(工厂模式)
- Android|Android 消息机制源码分析
- 设计模式|GO 常用设计模式
- 面试官(Kafka|面试官:Kafka 为什么会丢消息())
- 你想知道哪种减肥方法2
- java设计模式————委派模式,天天使用却不知道的模式,手撸spring
- 郑州也有动漫展你知道吗()
- 吼孩子时,你知道孩子的感受吗()