原型模式(Prototype Pattern)是用於建立重複的對象,同時又能保證性能。這種類型的設計模式屬於建立型模式,它提供了一種建立對象的最佳方式。java
這種模式是實現了一個原型接口,該接口用於建立當前對象的克隆。當直接建立對象的代價比較大時,則採用這種模式。例如,一個對象須要在一個高代價的數據庫操做以後被建立。咱們能夠緩存該對象,在下一個請求時返回它的克隆,在須要的時候更新數據庫,以此來減小數據庫調用。程序員
protected native Object clone() throws CloneNotSupportedException;
由方法聲明的放回值類型=>native,可知該方法其實是一個C++封裝好的方法,由java來進行調用,至關於C++語言的複製構造函數,可是又有所區別。數據庫
package com.xgp.company.建立型模式.第四種_原型模式.demo1; import java.util.Date; /** * 一、實現一個接口 * 二、重寫一個方法 */ //視頻原型類 public class Video implements Cloneable { private String name; private Date createTime; /** * 重寫克隆方法 * @return * @throws CloneNotSupportedException */ @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } 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 + '}'; } }
package com.xgp.company.建立型模式.第四種_原型模式.demo1; import java.util.Date; /** * 客戶端:克隆 */ public class Client { public static void main(String[] args) throws CloneNotSupportedException { //原型對象 Video v1 = new Video("狂神說java", new Date()); System.out.println("v1 = " + v1); System.out.println("v1 = " + v1.hashCode()); System.out.println("========================"); //v1 克隆 v2 Video v2 = (Video) v1.clone(); System.out.println("v2 = " + v2); System.out.println("v2 = " + v2.hashCode()); } }
v1 = Video{name='狂神說java', createTime=Fri Feb 14 10:26:57 CST 2020} v1 = 1836019240 ======================== v2 = Video{name='狂神說java', createTime=Fri Feb 14 10:26:57 CST 2020} v2 = 325040804
/** * 演示淺克隆弊端 * @param args * @throws CloneNotSupportedException */ public static void main(String[] args) throws CloneNotSupportedException { //原型對象 Date date = new Date(); Video v1 = new Video("狂神說java", date); System.out.println("v1 = " + v1); System.out.println("v1 = " + v1.hashCode()); //v1 克隆 v2 Video v2 = (Video) v1.clone(); System.out.println("v2 = " + v2); System.out.println("v2 = " + v2.hashCode()); System.out.println("========================"); date.setTime(22222222); System.out.println("v1 = " + v1); System.out.println("v1 = " + v1.hashCode()); //v1 克隆 v2 System.out.println("v2 = " + v2); System.out.println("v2 = " + v2.hashCode()); }
v1 = Video{name='狂神說java', createTime=Fri Feb 14 10:29:02 CST 2020} v1 = 1836019240 v2 = Video{name='狂神說java', createTime=Fri Feb 14 10:29:02 CST 2020} v2 = 325040804 ======================== v1 = Video{name='狂神說java', createTime=Thu Jan 01 14:10:22 CST 1970} v1 = 1836019240 v2 = Video{name='狂神說java', createTime=Thu Jan 01 14:10:22 CST 1970} v2 = 325040804
@Override protected Object clone() throws CloneNotSupportedException { /** * 改造克隆方法,進行生克隆 */ Object obj = super.clone(); Video v = (Video) obj; //將對象的屬性進行克隆 v.createTime = (Date) this.createTime.clone(); return obj; }
v1 = Video{name='狂神說java', createTime=Fri Feb 14 10:37:33 CST 2020} v1 = 1836019240 v2 = Video{name='狂神說java', createTime=Fri Feb 14 10:37:33 CST 2020} v2 = 325040804 ======================== v1 = Video{name='狂神說java', createTime=Thu Jan 01 14:10:22 CST 1970} v1 = 1836019240 v2 = Video{name='狂神說java', createTime=Fri Feb 14 10:37:33 CST 2020} v2 = 325040804