原型模式
原型模式并没有例如观察者或者工厂模式那样的常用,但是当我们对对象的属性需要模块化的变动的时候,虽然我们有很多种方式可以对这一操作进行实现,但是我认为这个时候是原型模式最佳的应用场景。好处有几个:第一,我们不需要一直去操作一个对象,可以防止程序可能发生的很多的未知的问题。第二,我们可以进行模块化变动,每次变动后都是一个全新的对象,而之前的对象依然保存在程序中。第三,不需要去创建新的对象,节省内存和程序的开销。
对于原型模式有一个非常经典的场景:简历的复印。
代码如下:
1 package com.sunyard.bean; 2 3 import java.util.Map; 4 5 /** 6 * <p>简历类</p> 7 * @author:774346810@qq.com 8 * @date:2017-6-15 9 */ 10 public class Resume implements Cloneable{ 11 private String name;//姓名 12 private String school;//学校 13 private String graduateYear;//毕业年份 14 private int age;//年龄 15 16 private String address;//地址 17 private String company;//公司 18 private String timeArea;//工作年限范围 19 20 //设置个基本信息 21 public void setPersonInfo(Map<String, Object> params){ 22 String name = (String) params.get("name"); 23 String school = (String) params.get("school"); 24 String graduateYear = (String) params.get("graduateYear"); 25 int age = (Integer) params.get("age"); 26 setName(name); 27 setSchool(school); 28 setGraduateYear(graduateYear); 29 setAge(age); 30 } 31 32 //设置工作经历 33 public void setWorkInfo(Map<String, Object> params){ 34 String address = (String) params.get("address"); 35 String company = (String) params.get("company"); 36 String timeArea = (String) params.get("timeArea"); 37 setAddress(address); 38 setCompany(company); 39 setTimeArea(timeArea); 40 } 41 42 @Override 43 public String toString() { 44 return "Resume [name=" + name + ", school=" + school 45 + ", graduateYear=" + graduateYear + ", age=" + age 46 + ", address=" + address + ", company=" + company 47 + ", timeArea=" + timeArea + "]"; 48 } 49 50 public String getName() { 51 return name; 52 } 53 public void setName(String name) { 54 this.name = name; 55 } 56 public String getSchool() { 57 return school; 58 } 59 public void setSchool(String school) { 60 this.school = school; 61 } 62 public void setGraduateYear(String graduateYear) { 63 this.graduateYear = graduateYear; 64 } 65 public String getGraduateYear() { 66 return graduateYear; 67 } 68 public String getAddress() { 69 return address; 70 } 71 public void setAddress(String address) { 72 this.address = address; 73 } 74 public int getAge() { 75 return age; 76 } 77 public void setAge(int age) { 78 this.age = age; 79 } 80 public String getCompany() { 81 return company; 82 } 83 public void setCompany(String company) { 84 this.company = company; 85 } 86 public String getTimeArea() { 87 return timeArea; 88 } 89 public void setTimeArea(String timeArea) { 90 this.timeArea = timeArea; 91 } 92 93 @Override 94 protected Object clone() throws CloneNotSupportedException { 95 return super.clone(); 96 } 97 }
1 package com.sunyard.bean; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class ResumeTest { 7 public static void main(String[] args) throws CloneNotSupportedException { 8 Map<String, Object> params = new HashMap<String, Object>(); 9 params.put("name", "张三"); 10 params.put("school", ""); 11 params.put("graduateYear", "2005"); 12 params.put("age", 24); 13 params.put("address", "杭州是滨江区"); 14 params.put("company", "信雅达系统工程股份有限公司"); 15 params.put("timeArea", "2005-2010"); 16 17 Resume resume = new Resume(); 18 resume.setPersonInfo(params); 19 resume.setWorkInfo(params); 20 21 System.out.println("在信雅达时候的张三:" + resume); 22 23 //张三跳槽了,去了恒生电子工作了三年 24 params.put("address", "杭州市滨江区"); 25 params.put("company", "恒生电子股份有限公司"); 26 params.put("timeArea", "2010-2013"); 27 28 Resume resume2 = (Resume) resume.clone(); 29 resume2.setWorkInfo(params); 30 31 System.out.println("在恒生电子时候的张三:" + resume2); 32 33 //后来张三又去了网易,工作到现在 34 Resume resume3 = (Resume) resume2.clone(); 35 params.put("address", "杭州市滨江区"); 36 params.put("company", "网易"); 37 params.put("timeArea", "2013-2017"); 38 resume3.setWorkInfo(params); 39 40 System.out.println("在网易时候的张三:" + resume3); 41 } 42 }
1 在信雅达时候的张三:Resume [name=张三, school=华东交通大学, graduateYear=2005, age=24, address=杭州是滨江区, company=信雅达系统工程股份有限公司, timeArea=2005-2010] 2 在恒生电子时候的张三:Resume [name=张三, school=华东交通大学, graduateYear=2005, age=24, address=杭州市滨江区, company=恒生电子股份有限公司, timeArea=2010-2013] 3 在网易时候的张三:Resume [name=张三, school=华东交通大学, graduateYear=2005, age=24, address=杭州市滨江区, company=网易, timeArea=2013-2017]