The builder pattern -java

First, the concept model builder

  • The builder pattern: object creation software design patterns, which aims to find the anti-telescoping constructor pattern solution

For example, we have the following constructor

public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
}
复制代码

You can see the parameters of a constructor is very large, the future may also continue to add, then the time after using the constructor's difficult to understand the order parameter, which can be called anti-telescoping constructor pattern.

Therefore, the purpose of the builder pattern is to construct a complex object from its representation separately, so that the same construction process can create different representations.

In simple terms, the builder mode allows you to create different styles of objects, while avoiding the constructor pollution, which is useful when there are multiple constructors or create objects involves many steps.

Second, actual examples

public class QueryPaginationResponse implements Serializable {
	/**
	 *查询是否成功 1,0 
 	 */
	private int status;
	/**
	 * 当前页
	 */
	private int current;
	/**
	 *总页数 
 	 */
	private int total;
	/**
	 *  总记录数
	 */
	private int count;
	/**
	 * 一页的记录数
	 */
	private int pageSize;
	/**
	 * 得到的数据
	 */
	private List<Object> list; 

	public QueryPaginationResponse(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}

	public static class Builder{
		private QueryPaginationResponse instance;

		public Builder(QueryPaginationResponse q){
			this.instance = q;
		}
		public Builder(int status){
			this(new QueryPaginationResponse(status));
		}

		public Builder setCurrent(int current){
			this.instance.current = current;
			return this;
		}

		public Builder setTotal(int total){
			this.instance.total = total;
			return this;
		}
		public Builder setCount(int count){
			this.instance.count = count;
			return this;
		}

		public Builder setPageSize(int pageSize){
			this.instance.pageSize = pageSize;
			return this;
		}

		public Builder setList(List<Object> list){
			this.instance.list = list;
			return this;
		}

		public  QueryPaginationResponse build(){
			return this.instance;
		}
	}
}

QueryPaginationResponse response = new QueryPaginationResponse.Builder(1).setCount(100)
    .setCurrent(1).setTotal(2).setPageSize(50).setList(new ArrayList<>()).build();

System.out.println(response.toString());
复制代码

When ever we need a new QueryPaginationResponse objects, they often call the constructor directly generate an object, such as:

public QueryPaginationResponse(int status, int current, int total, int count) {
    this.status = status;
    this.current = current;
    this.total = total;
    this.count = count;
}
复制代码

When a member of the four properties only when the constructor only four parameters. But when you add a member property pageSize, then you must modify the constructor, or overloaded constructors

public QueryPaginationResponse(int status, int current, int total, int count, int pageSize) {
    this.status = status;
    this.current = current;
    this.total = total;
    this.count = count;
    this.pageSize = pageSize;
}
复制代码

If the added attribute, then you have to repeat the above steps, modifying or overload:

public QueryPaginationResponse(int status, int current, int total, int count, int pageSize, List<Object> list) {
    this.status = status;
    this.current = current;
    this.total = total;
    this.count = count;
    this.pageSize = pageSize;
    this.list = list;
}
复制代码

This has brought great inconvenience Builder for the calling code can solve this complex object constructor logic.

Can be seen from the above examples show the code inside QueryPaginationResponse class, we create an internal class Builder, comprising a QueryPaginationResponse inner Builder class attribute type. Internal Builder class has two constructors, its purpose is to instantiate QueryPaginationResponse types of attributes.

public static class Builder {
    private QueryPaginationResponse instance;
	public Builder(QueryPaginationResponse q){this.instance = q;}
    public Builder(int status){
        this(new QueryPaginationResponse(status));
    }

复制代码

After the object is to create QueryPaginationResponse assignment method for each property, and method names at random, but these methods are assigned to the successive calls Builder objects, so the internal method returns the Builder objects have to return this ;.

public Builder setTotal(int total){
    // 实质给QueryPaginationResponse实例赋值
    this.instance.total = total;
    return this;
}

public Builder setCount(int count){
    this.instance.count = count;
    return this;
}

public Builder setPageSize(int pageSize){
    this.instance.pageSize = pageSize;
    return this;
}

public Builder setList(List<Object> list){
    this.instance.list = list;
    return this;
}

复制代码

The last statement of a method instantiates the Builder inside out will be able to return to class

public QueryPaginationResponse build(){
    return this.instance;
}
复制代码

The final code is like this, using the Builder design mode. When we need to create different styles of objects when no longer needed overloaded constructor, and only need to determine which evaluation method Builder instance to call it.

// 给5个属性的QueryPaginationResponse实例
QueryPaginationResponse response = new QueryPaginationResponse.Builder(1).setCount(100)
    .setCurrent(1).setTotal(2).setPageSize(50).setList(new ArrayList<>()).build();

// 给4个属性的QueryPaginationResponse实例
QueryPaginationResponse response = new QueryPaginationResponse.Builder(1).setCount(100)
    .setCurrent(1).setTotal(2).setPageSize(50).build();

复制代码

Reproduced in: https: //juejin.im/post/5d0751fdf265da1b5d57ac32

Guess you like

Origin blog.csdn.net/weixin_33887443/article/details/93166277