[Code Piece] Advanced replacement of set() in pojo

     Before, writing pojo classes was probably like this:

      piece - 01

package com.basecode;

//import java.nio.file.attribute.AclEntry.Builder;

/**
 * Old Java object pojo
 * @author vincent
 *
 */
public class EmpTest {
	private Integer id;  
	private Integer empLevel; //Employee level
	private String mappingOrderLevel; //Corresponding order level
	private String empNo; //Employee number
	private Integer orderNumLimit; //Each order goes online
	
	//1
	private EmpTest(Builder builder){
		setId(builder.id);
        setEmpLevel(builder.empLevel);
        setMapingOrderLevel(builder.mapingOrderLevel);
        setEmpNo(builder.empNo);
        setOrderNumLimit(builder.orderNumLimit);
	}
	
	//2
	public static Builder newBuilder(){
		return new Builder();
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getEmpLevel() {
		return empLevel;
	}

	public void setEmpLevel(Integer empLevel) {
		this.empLevel = empLevel;
	}

	public String getMapingOrderLevel() {
		return mapingOrderLevel;
	}

	public void setMapingOrderLevel(String mapingOrderLevel) {
		this.mapingOrderLevel = mapingOrderLevel;
	}

	public String getEmpNo () {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public Integer getOrderNumLimit() {
		return orderNumLimit;
	}

	public void setOrderNumLimit(Integer orderNumLimit) {
		this.orderNumLimit = orderNumLimit;
	}
	
	//3
	public static final class Builder{
		private Integer id;
		private Integer empLevel;
		private String mapingOrderLevel;
		private String empNo;
		private Integer orderNumLimit;
		
		public Builder(){
		}
		
		public Builder id(Integer val){
			id = val;
			return this;
		}
		
		public Builder empLevel(Integer val){
			empLevel = val;
			return this;
		}
		
		public Builder mapingOrderLevel(String val) {
            mapingOrderLevel = val;
            return this;
        }
		
		public Builder empNo(String val) {
            empNo = val;
            return this;
        }
		
		public Builder orderNumLimit(Integer val) {
            orderNumLimit = val;
            return this;
        }
		
		public EmpTest build() {
            return new EmpTest(this);
        }
	}
}


      Let's analyze the above code for the time being. Normally, it will be written as:

       piece - 02

/**
 * employee pojo -- usually written
 * @author vincent
 *
 */
public class EmpTest {
	private Integer id;  
	private Integer empLevel; //Employee level
	private String mappingOrderLevel; //Corresponding order level
	private String empNo; //Employee number
	private Integer orderNumLimit; //Each order goes online
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getEmpLevel() {
		return empLevel;
	}

	public void setEmpLevel(Integer empLevel) {
		this.empLevel = empLevel;
	}

	public String getMapingOrderLevel() {
		return mapingOrderLevel;
	}

	public void setMapingOrderLevel(String mapingOrderLevel) {
		this.mapingOrderLevel = mapingOrderLevel;
	}

	public String getEmpNo () {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public Integer getOrderNumLimit() {
		return orderNumLimit;
	}

	public void setOrderNumLimit(Integer orderNumLimit) {
		this.orderNumLimit = orderNumLimit;
	}
}
      In "piece - 01", I marked the difference between "piece - 01" and "piece - 02" by commenting //1, 2, 3. The following is a detailed analysis of the specific writing of "piece - 01".

      A few things to note:

      1. There is an internal static class "Builder" in the EmpTest class.

      2. The static method "newBuilder()" in the EmpTest class returns an instance of the inner class.

      3. The construction method of the EmpTest class, enter the Builder object, and assign values ​​to the properties of EmpTest respectively.

      4. The constructor of the internal class Builder is shared.

      5. The "build()" method in the inner class Builder returns an instance of the outer class EmpTest.

      Let's draw a process first:

  

     

      Using the Pojo class:

      The information of these 50 employees is displayed in the requirement --> main() method.

       piece - 03

public class SplitMark {

	public static List<EmpTest> empList;
	
	//initialize when class is loaded
	static{
		empList = new ArrayList<>();
		orderList = new ArrayList<>();
		
		for(int i=0;i<50;i++){
			empList.add(EmpTest.newBuilder().empLevel(getRandom(5)).empNo("Emp_"+i).id(i)
					.mapingOrderLevel(getRandomChar()).orderNumLimit(getRandom(100)).build());
		}
	}
	
	public static void main(String[] args) {
		for(int i=0;i<50;i++){
			System.out.println(empList.get(i).toString()); //Direct object.toString(), needs to be rewritten
		}
	}
	
	//The employee level is defined as between 0 and a
	public static Integer getRandom(int a) {
        Random random = new Random();
        return random.nextInt(a);
    }
	
	//The order level is simulated as four levels of A, B, C, D
	public static String getRandomChar() {
        String chars = "ABCD";
        return String.valueOf(chars.charAt((int)(Math.random() * 4)));
    }
}
       As above, the code in piece - 03, the JVM class loading mechanism has been studied, and this part of the code in the static{} block will assign an initial value to the static List . Before void main() is executed, the information of 50 employees and the assignment are completed. .

       It should be noted that at this time, the pojo call to EmpTest is written as follows:

       piece - 04

for(int i=0;i<50;i++){
	empList.add(EmpTest.newBuilder().empLevel(getRandom(5)).empNo("Emp_"+i).id(i)
			.mapingOrderLevel(getRandomChar()).orderNumLimit(getRandom(100)).build());
}
      Focus on this sentence:

      piece -05

EmpTest.newBuilder().empLevel(getRandom(5)).empNo("Emp_"+i).id(i)
	.mapingOrderLevel(getRandomChar()).orderNumLimit(getRandom(100)).build();

     Structure: POJO class.newBuilder().Inner class method().Inner class method().......build()

     Discover:

     1. The "EmpTest" class is not instantiated.

     2. Not using the traditional set() method to assign values ​​to its properties.

     3. After the build() method of the inner class Build is called, a pojo instance is created, and the assignment is completed.


       Combined with piece - 03 - 04 - 05, look back at the picture I drew and the code in piece - 01. This way of using POJO is very clever. In fact, at this time, the entity class is no longer a POJO class . Modified the structure of the entity class, the constructor of the outer class becomes the link between the inner and outer classes, and the build() method of the inner class directly returns an instance of the outer class, (provided that the attributes of the outer class and inner class are consistent ) and then use it, attribute assignment, it is very convenient.


      The attribute assignment in the ES source code is written in this way, and the team leader also suggested that I write it in this way, and I will share it with you.














Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325624854&siteId=291194637