Java设计模式之------值对象模式

值对象模式


在Java开发时,需要来回交换大量的数据,比如要为方法传入参数,也要获取方法的返回值

值对象的本质是“封装数据”。值对象模式在开发中用的很多,要熟练掌握。

基本的编写步骤:

第1步:写一个类,实现可序列化(如果以后数据是往数据库里存的,那么可以不序列化,节省资源)
第2步:私有化所有属性,保持一个默认构造方法(public无参)
第3步:为每个属性提供get()、set()方法(如果是boolean型变量,最好把get改成is)
第4步:推荐覆盖实现equals()、hashCode()和toString()方法


普通的值传递:
如果只是普通的值传递是不需要实现serializable接口的
[java]  view plain  copy
  1. package cn.hncu.pattern.vo.v1;  
  2.   
  3. public class A {  
  4.   
  5.     public static void main(String[] args) {  
  6.         B obj = new B();  
  7.         obj.sendData("A001""13512345678""hncu",20);  
  8.         System.out.println( obj.getUserId() );  
  9.     }  
  10.   
  11. }  

[java]  view plain  copy
  1. package cn.hncu.pattern.vo.v1;  
  2.   
  3. public class B {  
  4.     String userId=null;  
  5.    public boolean sendData(String userId,String tel,String address,int age){  
  6.        System.out.println("数据已经接收到....");  
  7.        operate(userId,tel,address,age);  
  8.        return true;  
  9.    }  
  10.   
  11.     private void operate(String userId, String tel, String address, int age) {  
  12.         this.userId = "CN_"+userId;  
  13.         System.out.println("处理数据....");  
  14.     }  
  15.   
  16.     public String getUserId() {  
  17.         return userId;  
  18.     }  
  19.       
  20. }  

对象的值传递
如果传递的是对象,那么该类的类模板需要实现serializable接口实现序列化,因为对象是有生命期的,会消亡,所以需要用序列化使其再生
[java]  view plain  copy
  1. package cn.hncu.pattern.vo.v2.vo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class User implements Serializable{//只能将支持 java.io.Serializable 接口的对象写入流中  
  6.     private String userId;  
  7.     private String tel;  
  8.     private String address;  
  9.     private int age;  
  10.     private boolean isMale;  
  11.       
  12.     public User(String userId, String tel, String address, int age) {  
  13.         super();  
  14.         this.userId = userId;  
  15.         this.tel = tel;  
  16.         this.address = address;  
  17.         this.age = age;  
  18.     }  
  19.     public User(){  
  20.           
  21.     }  
  22.       
  23.     public String getUserId() {  
  24.         return userId;  
  25.     }  
  26.     public void setUserId(String userId) {  
  27.         this.userId = userId;  
  28.     }  
  29.     public String getTel() {  
  30.         return tel;  
  31.     }  
  32.     public void setTel(String tel) {  
  33.         this.tel = tel;  
  34.     }  
  35.     public String getAddress() {  
  36.         return address;  
  37.     }  
  38.     public void setAddress(String address) {  
  39.         this.address = address;  
  40.     }  
  41.     public int getAge() {  
  42.         return age;  
  43.     }  
  44.     public void setAge(int age) {  
  45.         this.age = age;  
  46.     }  
  47.       
  48.     public boolean isMale() {  
  49.         return isMale;  
  50.     }  
  51.     public void setMale(boolean isMale) {  
  52.         this.isMale = isMale;  
  53.     }  
  54.     @Override  
  55.     public int hashCode() {  
  56.         final int prime = 31;  
  57.         int result = 1;  
  58.         result = prime * result + ((userId == null) ? 0 : userId.hashCode());  
  59.         return result;  
  60.     }  
  61.     @Override  
  62.     public boolean equals(Object obj) {  
  63.         if (this == obj)  
  64.             return true;  
  65.         if (obj == null)  
  66.             return false;  
  67.         if (getClass() != obj.getClass())  
  68.             return false;  
  69.         User other = (User) obj;  
  70.         if (userId == null) {  
  71.             if (other.userId != null)  
  72.                 return false;  
  73.         } else if (!userId.equals(other.userId))  
  74.             return false;  
  75.         return true;  
  76.     }  
  77.     @Override  
  78.     public String toString() {  
  79.         return "User [userId=" + userId + ", tel=" + tel + ", address="  
  80.                 + address + ", age=" + age + "]";  
  81.     }  
  82.       
  83. }  
[java]  view plain  copy
  1. package cn.hncu.pattern.vo.v2;  
  2.   
  3. import cn.hncu.pattern.vo.v2.vo.User;  
  4.   
  5. public class B {  
  6.     String userId=null;  
  7.     User user = null;  
  8.    public boolean sendData(User user){ //Value Object ----Model  
  9.        System.out.println("数据已经接收到....");  
  10.        operate(user);  
  11.        return true;  
  12.    }  
  13.   
  14.     private void operate(User user) {  
  15.         this.user = user;  
  16.         this.user.setUserId("CN_"+user.getUserId());  
  17.         System.out.println("处理数据....");  
  18.     }  
  19.   
  20.     public String getUserId() {  
  21.         return userId;  
  22.     }  
  23.       
  24.     public User getData(){  
  25.         return user;  
  26.     }  
  27.       
  28. }  
[java]  view plain  copy
  1. package cn.hncu.pattern.vo.v2;  
  2.   
  3. import cn.hncu.pattern.vo.v2.vo.User;  
  4.   
  5. public class A {  
  6.   
  7.     public static void main(String[] args) {  
  8.         B obj = new B();  
  9.         User user = new User("A001""13512345678""hncu",20);  
  10.           
  11.         obj.sendData(user);  
  12.         User data = obj.getData();  
  13.           
  14.         System.out.println( data.getUserId() );  
  15.           
  16.           
  17.         User user2 = new User();  
  18.         user2.setAge(21);  
  19.         //唱票模型2318,2319,......  
  20.           
  21.     }  
  22.   
  23. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值