原型模式的定义
原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。 [1]
解决问题
它主要面对的问题是:“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是他们却拥有比较稳定一致的接口。
实战:
案例一:浅克隆
package coms.Design.Mode.Prototype.Demo01; import java.util.Date; /** * 视频原型 * */ public class Video implements Cloneable { private String name; private Date createTime; public Video() { } public Video(String name, Date createTime) { this.name = name; this.createTime = createTime; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { return "Video{" + "name='" + name + '\'' + ", createTime=" + createTime + '}'; } /** * 克隆技术 * @return * @throws CloneNotSupportedException */ @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
package coms.Design.Mode.Prototype.Demo01; import com.JavaBasicsDemo7.Interface1.D; /** * 浅克隆 */ import java.util.Date; public class Bili { public static void main(String[] args) throws CloneNotSupportedException { //原型对象 Date date = new Date(); Video video1 = new Video("在学java的23设计者模式开发", date); System.out.println("v1===>"+video1); System.out.println("v1====>hash:"+video1.hashCode()); //v1复制到v2 Video video2=(Video)video1.clone(); //克隆的对象 System.out.println("v2==========>"+video2); System.out.println("v2====>hash"+video2.hashCode()); video2.setName("Clone:讲义"); System.out.println(video2); } }
案例二:深克隆
package coms.Design.Mode.Prototype.Demo02; import java.util.Date; public class Video implements Cloneable { private String name; private Date createTime; public Video() { } /** * 克隆技术 * * @return * @throws CloneNotSupportedException */ @Override protected Object clone() throws CloneNotSupportedException { Object obj = super.clone(); //将这个对象的属性页克隆进去 //深克隆 Video v = (Video) obj; v.createTime = (Date) this.createTime.clone(); return obj; } public Video(String name, Date createTime) { this.name = name; this.createTime = createTime; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { return "Video{" + "name='" + name + '\'' + ", createTime=" + createTime + '}'; } }
package coms.Design.Mode.Prototype.Demo02; /** * 浅克隆 * Spring Bean 单例模式 原型模式 */ import java.util.Date; public class Bili { public static void main(String[] args) throws CloneNotSupportedException { //原型对象 Date date = new Date(); Video video1 = new Video("在学java的23设计者模式开发", date); Video video2=(Video)video1.clone(); //克隆的对象 System.out.println("v1===>"+video1); System.out.println("v1====>hash:"+video1.hashCode()); date.setTime(2222); //v1复制到v2 System.out.println("v2==========>"+video2); System.out.println("v2====>hash"+video2.hashCode()); video2.setName("Clone:讲义"); System.out.println(video2); } }