值对象模式
在Java开发时,需要来回交换大量的数据,比如要为方法传入参数,也要获取方法的返回值
值对象的本质是“封装数据”。值对象模式在开发中用的很多,要熟练掌握。
基本的编写步骤:
第1步:写一个类,实现可序列化(如果以后数据是往数据库里存的,那么可以不序列化,节省资源)
第2步:私有化所有属性,保持一个默认构造方法(public无参)
第3步:为每个属性提供get()、set()方法(如果是boolean型变量,最好把get改成is)
第4步:推荐覆盖实现equals()、hashCode()和toString()方法
普通的值传递:
如果只是普通的值传递是不需要实现serializable接口的
- package cn.hncu.pattern.vo.v1;
- public class A {
- public static void main(String[] args) {
- B obj = new B();
- obj.sendData("A001", "13512345678", "hncu",20);
- System.out.println( obj.getUserId() );
- }
- }
- package cn.hncu.pattern.vo.v1;
- public class B {
- String userId=null;
- public boolean sendData(String userId,String tel,String address,int age){
- System.out.println("数据已经接收到....");
- operate(userId,tel,address,age);
- return true;
- }
- private void operate(String userId, String tel, String address, int age) {
- this.userId = "CN_"+userId;
- System.out.println("处理数据....");
- }
- public String getUserId() {
- return userId;
- }
- }
如果传递的是对象,那么该类的类模板需要实现serializable接口实现序列化,因为对象是有生命期的,会消亡,所以需要用序列化使其再生
- package cn.hncu.pattern.vo.v2.vo;
- import java.io.Serializable;
- public class User implements Serializable{//只能将支持 java.io.Serializable 接口的对象写入流中
- private String userId;
- private String tel;
- private String address;
- private int age;
- private boolean isMale;
- public User(String userId, String tel, String address, int age) {
- super();
- this.userId = userId;
- this.tel = tel;
- this.address = address;
- this.age = age;
- }
- public User(){
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String userId) {
- this.userId = userId;
- }
- public String getTel() {
- return tel;
- }
- public void setTel(String tel) {
- this.tel = tel;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public boolean isMale() {
- return isMale;
- }
- public void setMale(boolean isMale) {
- this.isMale = isMale;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((userId == null) ? 0 : userId.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;
- User other = (User) obj;
- if (userId == null) {
- if (other.userId != null)
- return false;
- } else if (!userId.equals(other.userId))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "User [userId=" + userId + ", tel=" + tel + ", address="
- + address + ", age=" + age + "]";
- }
- }
- package cn.hncu.pattern.vo.v2;
- import cn.hncu.pattern.vo.v2.vo.User;
- public class B {
- String userId=null;
- User user = null;
- public boolean sendData(User user){ //Value Object ----Model
- System.out.println("数据已经接收到....");
- operate(user);
- return true;
- }
- private void operate(User user) {
- this.user = user;
- this.user.setUserId("CN_"+user.getUserId());
- System.out.println("处理数据....");
- }
- public String getUserId() {
- return userId;
- }
- public User getData(){
- return user;
- }
- }
- package cn.hncu.pattern.vo.v2;
- import cn.hncu.pattern.vo.v2.vo.User;
- public class A {
- public static void main(String[] args) {
- B obj = new B();
- User user = new User("A001", "13512345678", "hncu",20);
- obj.sendData(user);
- User data = obj.getData();
- System.out.println( data.getUserId() );
- User user2 = new User();
- user2.setAge(21);
- //唱票模型2318,2319,......
- }
- }