Java --- Design Patterns --- Value Objects

Scenarios and Questions

    When developing in Java, a large amount of data needs to be exchanged back and forth. For example, to pass in parameters for the method, and also to obtain the return value of the method, how to better interact with the data?

Basic writing steps

    ◎Step 1: Write a class to realize serialization (if the data is stored in the database in the future, it can be not serialized to save resources)
    ◎Step 2: Privateize all attributes and keep a default constructor (public No parameters)
    ◎Step 3: Provide get() and set() methods for each attribute (if it is a boolean variable, it is best to change get to is)
    ◎Step 4: It is recommended to override equals(), hashCode( ) and the toString() method

The essence of value objects is to "encapsulate data"
import java.io.Serializable;
/**
 * Time:2018/4/15
 * Description:
 * Value object naming convention: xxxValueObject, xxxVO, xxxModel
 * Or directly name xxx under a stored value object package,
 * Steps:
 * 1. Implement serialization interface
 * 2. Private member variables
 * 3. Keep a null parameter constructor
 * 4. Write set and get methods for privatized member variables. Note: the get method of boolean type is isXxx.
 * 5. Write equal and hashCode methods
 * 6. Write the toString method.
 * @author Song Jinyu
 */
//It is better to implement the serialization interface
public class UserModel implements Serializable {
	private String id;//id is generally set as the primary key
	private String userName;//User name
	private Integer age;//age
	private String pwd;//Password
	private boolean admin; //Whether it is an administrator boolean variable name, do not take "isXxx"
						   //Because the generated geter function is isXxx(), there will be problems in the framework in the future.
	//Remember to keep a null parameter construct.
	public UserModel() {
	}
	
	public boolean isAdmin() {
		return admin;
	}
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserModel other = (UserModel) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return id + ", " + userName + ", " + age ;
	}
}


Guess you like

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