原型模式: 用原型实例指定传经对象的种类,并且通过拷贝这些原型创建新的对象。其实就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节。
根据上一篇 《浅拷贝和深拷贝》的内容,我们知道,浅拷贝后原对象(A)和复制出来的对象(B)中对其他对象的引用都是相同的。如果复制出来的对象(B)改变了其保存的其他对象(C)的引用内容,那么这个原对象(A)对保存的其他对象(C)也会跟着改变。
使用原型模式的目的,就是希望能够复制出多个对象,并且每个对象都是独立存在,每个对象所引用的对象也没有和其他对象共享。
结合<大话设计模式>中的复制简历的例子来讨论,为什么需要使用原型模式:
搞java 的在找工作时都需要投递简历,不同的公司对java 程序员的要求因为具体的职责不同要求也不同。我在投实习简历的时候,一般会关注这个公司的岗位要求,例如熟悉Mysql ,javascript ,面向对象知识等等。每次我就对症下药对应着这些要求去填写我的建立,其他的知识技能就不填写了。不过每个公司对java 程序员的要求大体相同,因此每次我只需要把拷贝一份简历,将这份简历修改成对方感兴趣的内容就可以了,花费不了多长时间。在这个过程中,就使用到了原型模式,否则,每次都要重新敲打出一份简历,费时费事啊。
使用原型模式的好处就是拷贝,对拷贝出来对象进行简单修改就可以满足需求,并且无论怎么做修改都不影响到原来的对象。
根据上面的讨论,发现原型模式和深拷贝有着很深的关系,的确,原型模式就是用到了深拷贝来进行对象的复制。
下面用简历程序来实现原型模式:
1. 建立一个获奖类,该类会被 MyResume 简历类引用到。
- /**
- * 获奖类,获奖类包括 时间 地点 所获奖项。 该获奖类是 MyResume 简历类的一个参数。
- * 获奖类有个构造函数,参数为传入 时间 地点 所获奖项
- * 获奖类重写了 toString 方法,用来返回获奖信息字符串
- * @author liaogang.pt
- */
- public class MyAward {
- private String date;
- private String name;
- private String location;
- public void setDate(String date) {
- this.date = date;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setLocation(String location) {
- this.location = location;
- }
- public MyAward(String date, String location, String name){
- this.date = date;
- this.location = location;
- this.name =name;
- }
- public String toString(){
- String awardStr ="";
- awardStr += "date : " +date
- + "laocation : "+location
- + "name: " +name;
- return awardStr;
- }
- }
/** * 获奖类,获奖类包括 时间 地点 所获奖项。 该获奖类是 MyResume 简历类的一个参数。 * 获奖类有个构造函数,参数为传入 时间 地点 所获奖项 * 获奖类重写了 toString 方法,用来返回获奖信息字符串 * @author liaogang.pt */ public class MyAward { private String date; private String name; private String location; public void setDate(String date) { this.date = date; } public void setName(String name) { this.name = name; } public void setLocation(String location) { this.location = location; } public MyAward(String date, String location, String name){ this.date = date; this.location = location; this.name =name; } public String toString(){ String awardStr =""; awardStr += "date : " +date + "laocation : "+location + "name: " +name; return awardStr; } }
2 。简历简历类 MyResume 。简历类包括公司名称,技能,教育背景,获奖项等信息,同时简历类也重写了toString 方法,用来返回简历包含的信息。简历类还实现了Cloneable 接口,实现 clone 方法。
- /**
- * 这是一个原型类,也相当于 ConcretePrototype 类。
- * 重写了 clone 方法,能够实现深拷贝
- * @author liaogang.pt
- *
- */
- public class MyResume implements Cloneable{
- private String name ;
- private String skill;
- private String education;
- private MyAward award;
- public MyAward getAward() {
- return award;
- }
- private MyResume copy;
- public MyResume(MyResume child){
- this.copy = child;
- }
- public MyResume(String name,String skill,String education,MyAward award){
- this.name = name;
- this.skill = skill;
- this.education = education;
- this.award = award;
- }
- public String toString(){
- String wholeResumeStr ="";
- wholeResumeStr += "name: " + name +"/n"
- + "skill:" + skill +"/n"
- + "education:" + education +"/n"
- + "award:" + award.toString() +"/n" ;
- return wholeResumeStr;
- }
- public Object clone(){
- MyResume t = null;
- try{
- t = (MyResume)super.clone();
- }catch(CloneNotSupportedException e){
- e.printStackTrace();
- }
- return t;
- }
- public void setAward(MyAward award) {
- this.award = award;
- }
- public void setCopy(MyResume copy) {
- this.copy = copy;
- }
- public void setEducation(String education) {
- this.education = education;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setSkill(String skill) {
- this.skill = skill;
- }
- }
/** * 这是一个原型类,也相当于 ConcretePrototype 类。 * 重写了 clone 方法,能够实现深拷贝 * @author liaogang.pt * */ public class MyResume implements Cloneable{ private String name ; private String skill; private String education; private MyAward award; public MyAward getAward() { return award; } private MyResume copy; public MyResume(MyResume child){ this.copy = child; } public MyResume(String name,String skill,String education,MyAward award){ this.name = name; this.skill = skill; this.education = education; this.award = award; } public String toString(){ String wholeResumeStr =""; wholeResumeStr += "name: " + name +"/n" + "skill:" + skill +"/n" + "education:" + education +"/n" + "award:" + award.toString() +"/n" ; return wholeResumeStr; } public Object clone(){ MyResume t = null; try{ t = (MyResume)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } return t; } public void setAward(MyAward award) { this.award = award; } public void setCopy(MyResume copy) { this.copy = copy; } public void setEducation(String education) { this.education = education; } public void setName(String name) { this.name = name; } public void setSkill(String skill) { this.skill = skill; } }
3.建立一个测试类,用来验证程序的正确性:
这个测试类分别为3家公司准备了3份简历。其中第二份和第三份简历都是通过 clone方法建立的。
- public class TestPrototype {
- public static void main(String[] args ){
- String name = "A company";
- String skill = "javaScript, OOP, Mysql ,Flex";
- String education = "swjtu";
- MyAward awardA = new MyAward("2009-10-01","swjtu","third-price scholarship");
- MyResume forCompanyA = new MyResume(name,skill,education,awardA);
- String nameB ="B company";
- MyResume forCompanyB = (MyResume) forCompanyA.clone();
- forCompanyB.setName(nameB);
- MyResume forCompanyC =(MyResume) forCompanyB.clone();
- String nameC ="C company";
- forCompanyC.setName(nameC);
- forCompanyC.setSkill("Struts2 + Hibernate +Spring + design pattern");
- MyAward awardB =forCompanyC.getAward();
- awardB.setDate("2010-10-05");
- awardB.setName("NPMCM");
- System.out.println(forCompanyA.toString());
- System.out.println(forCompanyB.toString());
- System.out.println(forCompanyC.toString());
- }
- }