設計模式之原型模式(Prototype Pattern)
阿新 • • 發佈:2018-12-18
原型模式在我們平時工作中還是很常用的,而且使用起來非常簡單,其核心是一個clone方法。
- 原型模式的定義
Specify the kinds of objects to create using a prototypical instance,and create new objects by copying this prototype. 用原型例項指定建立物件的種類,並且通過拷貝這些原型建立新的物件。 - 原型模式的要點
- 原型模式主要用於物件的複製。
- 原型類Prototype必須實現Cloneable介面。在Java中提供了一個Cloneable介面,來標識這個物件是可以拷貝的,在JVM中具有這個標記的物件才有可能被拷貝。
- 重寫Object類中的clone方法。作用是返回物件的一個拷貝,但是其作用域protected型別的,一般的類無法呼叫。所以Prototype類需要將clone方法的作用域修改為public型別。
- 原型模式的通用類圖
- 原型模式的通用程式碼
原型類
public class Prototype implements Cloneable { /* (non-Javadoc) * @see java.lang.Object#clone() * * 重寫Object的clone()方法,必須是public * */ @Override public Prototype clone() { Protptype prototype = null; try { prototype = (Protptype) super.clone(); } catch (CloneNotSupportedException ex) { ex.printStackTrace(); } return prototype; } }
實現類
class ConcretePrototype extends Prototype{
public void show(){
System.out.println("原型模式實現類");
}
}
public class Client { public static void main(String[] args) { ConcretePrototype cp = new ConcretePrototype(); for (int i = 0; i < 10; i++) { ConcretePrototype clonecp = (ConcretePrototype) cp.clone(); clonecp.show(); } } }
執行結果
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
原型模式實現類
- 原型模式的例項類圖
在現實生活中有這樣一種場景,說起來也很煩。雙十一剛過,各種垃圾簡訊一頓狂轟亂炸,讓人很不爽。其實這種大批量的傳送簡訊的場景,原型模式是最適合不過了。
- 原型模式的例項程式碼
模板如下
package example;
public class Temeplate {
// 簡訊名字
private String subject = "【天貓雙11活動】";
private String content = "雙11活動通知:滿99減66.";
public String getSubject() {
return subject;
}
public String getContent() {
return content;
}
}
Message類如下
public class Message implements Cloneable {
private String receiver;
private String subject;
private String content;
public Message(Temeplate temeplate) {
this.subject = temeplate.getSubject();
this.content = temeplate.getContent();
}
@Override
public Message clone() {
Message msg = null;
try {
msg = (Message)super.clone();
}catch(CloneNotSupportedException ex) {
ex.printStackTrace();
}
return msg;
}
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 getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
實現類
public class Client {
private static int count = 10;
public static void main(String[] args) {
int i = 0;
Message msg = new Message(new Temeplate());
while (i < count) {
Message cloneMsg = msg.clone();
Random rand = new Random();
cloneMsg.setReceiver(rand.nextInt(1000000000) + 1+"@qq.com");
send(cloneMsg);
i++;
}
}
public static void send(Message msg) {
System.out.println(msg.getReceiver()+"接收成功");
}
}
輸出結果如下
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
[email protected]接收成功
這樣子就可以做到群發的效果,也就是商家只需要一份名單,就可以給名單裡的人傳送一樣的簡訊。
- 原型模式的注意事項
- 使用原型模式複製物件不會呼叫類的構造方法。因為物件的複製是通過呼叫Object類的clone方法來完成的,它直接在記憶體中複製資料,因此不會呼叫到類的構造方法。
- 深拷貝與淺拷貝。Object類的clone方法只會拷貝物件中的基本的資料型別(byte、char、short、int、long、float、double、boolean)。如果要實現深拷貝,必須將原型模式中的陣列、容器物件、引用物件等另行拷貝。
- clone和final是有衝突的。
- 原型模式的優點及適用場景
- 使用原型模式建立物件比直接new一個物件在效能上要好的多。
- 簡化物件的建立。
參考書籍:設計模式之禪
例項程式碼放在這裡。