**英文名稱:Prototype Pattern**
**定義**:用原型實例指定創建對象的種類,并且通過拷貝這些原型創建新的對象。
原型模式的核心就是一個clone方法,通過該方法進行對象的拷貝,java提供了一個Cloneable接口,來標示這個對象是可拷貝的。
**一個例子:**
一個銀行,定時給不同的用戶發送特定的郵件。
AdvTemplate類,advSubject(標題)advContext(內容)
Mail receiver(接收者),subject(名稱),appellation(稱謂),context(內容),tail(版本信息).
****
~~~
public class PrototypeTest {
public static void main(String[] args) {
//將模板放入到Mail實例中,發送郵件。
Mail mail = new Mail(new AdvTemplate());
for(int i=0;i<10000;i++){
Mail cloneMail = (Mail) mail.clone();
cloneMail.setAppellation(getRandString(5)+"先生:您好!\n");
cloneMail.setReceiver(getRandString(5)+".com\n");
sendMail(mail);
}
}
//發送信息,并返回已發送成功的回執。
public static void sendMail(Mail mail){
System.out.println("標題:"+mail.getSubject()+"\t 內容:"+mail.getContext()+"\t"+"發送成功!\n");
}
//隨機產生一些接收者和稱謂信息。
public static String getRandString(int length){
String source = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random random = new Random();
StringBuffer sb = new StringBuffer();
while(length-- != 0){
sb.append(source.charAt(random.nextInt(source.length())));
}
return sb.toString();
}
}
class AdvTemplate{
private String advSubject="某銀行元旦信用卡抽獎活動";
private String advContext="元旦抽獎活動,只要刷卡就送你一個大紅包!";
public String getAdvSubject() {
return advSubject;
}
public String getAdvContext() {
return advContext;
}
}
class Mail implements Cloneable{
//收件人
private String receiver;
//名稱
private String subject;
//稱謂
private String appellation;
//內容
private String context;
//版本信息
private String tail;
public Mail(AdvTemplate advTemplate) {
this.subject = advTemplate.getAdvSubject();
this.context = advTemplate.getAdvContext();
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getAppellation() {
return appellation;
}
public void setAppellation(String appellation) {
this.appellation = appellation;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getTail() {
return tail;
}
public void setTail(String tail) {
this.tail = tail;
}
//重寫Object類中的clone方法。
@Override
protected Object clone(){
Mail mail = null;
try {
mail = (Mail) super.clone();
} catch (Exception e) {
e.printStackTrace();
}
return mail;
}
}
~~~
**原型模型優點:**
性能優良,---原型模型是在內存二進制流的拷貝,比直接new一個對象性能好很多,特別是在循環體內產生大量的對象時,原型模式可以更好的體現其優點。
逃避構造函數的約束,--直接從內存中拷貝,構造函數是不會執行的。
**原型模式的注意事項**
1、構造函數不會被執行。
2、淺拷貝和深拷貝
**淺拷貝**
--|Object類提供的方法clone只是拷貝本對象,其對象內部的數組、引用對象等都不拷貝,還是指向原聲對象的內部元素地址,這種拷貝叫做淺拷貝。
**深拷貝**
--|兩個對象之間沒有任何瓜葛,你修改你的,我修改我的。互不影響,這種拷貝叫做深拷貝。
3、clone和final是冤家。
出現final的對象或變量,不能clone。
~~~
public class Prototype {
public static void main(String[] args) {
//創建一個對象
MyClone myClone = new MyClone();
myClone.setArrayList("lzl");
//將該對象clone。
MyClone clone2 = (MyClone) myClone.clone();
clone2.setArrayList("xy");
//輸出原對象的結果。
System.out.println("原對象...."+myClone.getArrayList());
//輸出拷貝后的結果。
System.out.println("拷貝結果...."+clone2.getArrayList());
}
}
class MyClone implements Cloneable{
private ArrayList<String> arrayList = new ArrayList<String>();
@SuppressWarnings("unchecked")
@Override
public MyClone clone(){
MyClone myClone =null;
try {
myClone = (MyClone) super.clone();
//把私有對象也進行拷貝。做到深拷貝的效果
myClone.arrayList = (ArrayList<String>) this.arrayList.clone();
} catch (Exception e) {
e.printStackTrace();
}
return myClone;
}
public ArrayList<String> getArrayList() {
return arrayList;
}
public void setArrayList(String name) {
this.arrayList.add(name);
}
}
~~~
- 前言
- 6大設計原則(一)---單一職責原則
- 6大設計原則(二)---里氏替換原則
- 6大設計原則(三)---依賴倒置原則
- 6大設計模式(四)----接口隔離原則
- 6大設計原則(五)---迪米特法則
- 6大設計原則(六)---開閉原則。
- 設計模式(一)---單例模式
- 設計模式(二)---工廠方法模式
- 設計模式(三)---抽象工廠模式
- 設計模式(四)---模板方法模式
- 設計模式(五)---建造者模式
- 設計模式(六)---代理模式
- 設計模式(七)---原型模式
- 設計模式(八)---中介者模式
- 設計模式(九)---命令模式
- 設計模式(十)---責任鏈模式
- 設計模式(十一)---裝飾模式
- 設計模式(十二)---策略模式
- 設計模式(十三)---適配器模式
- 設計模式(十四)---迭代器模式
- 設計模式(十五)---組合模式
- 設計模式(十六)---觀察者模式
- 設計模式(十七)---門面模式
- 設計模式(十八)---備忘錄模式
- 設計模式(十八)---訪問者模式
- 設計模式(二十)---狀態模式
- 設計模式(二十二)---享元模式
- 設計模式(二十三)---橋梁模式