您從圖書館的期刊從發(fā)現(xiàn)了幾篇您感興趣的文章,由於這是圖書館的書,您不可以直接在書中作記號(hào)或?qū)懽郑阅鷮?dāng)中您所感興趣的幾個(gè)主題影印出來,這下子您就可在影印的文章上畫記重點(diǎn)。
Prototype模式的作用有些類似上面的描述,您在父類別中定義一個(gè)clone()方法,而在子類別中重新定義它,當(dāng)客戶端對(duì)於所產(chǎn)生的物件有興趣並想加以利用,而您又不想破壞原來的物件,您可以產(chǎn)生一個(gè)物件的複本給它。
Prototype具有展示的意味,就像是展覽會(huì)上的原型車款,當(dāng)您對(duì)某個(gè)車款感興趣時(shí),您可以購(gòu)買相同款示的車,而不是車展上的車。
在軟體設(shè)計(jì)上的例子會(huì)更清楚的說明為何要進(jìn)行物件複製,假設(shè)您要設(shè)計(jì)一個(gè)室內(nèi)設(shè)計(jì)軟體,軟體中有一個(gè)展示家具的工具列,您只要點(diǎn)選工具列就可以產(chǎn)生一個(gè)家具複本,例如一張椅子或桌子,您可以拖曳這個(gè)複製的物件至設(shè)計(jì)圖中,隨時(shí)改變它的位置、顏色等等,當(dāng)您改變?cè)O(shè)計(jì)圖中的物件時(shí),工具列上的原型工具列是不會(huì)跟著一起改變的,這個(gè)道理是無需解釋的。
下面的 UML 類別圖表示了上述的簡(jiǎn)單概念:
Prototype模式的重點(diǎn)在於clone(),它負(fù)責(zé)複製物件本身並傳回,但這個(gè)clone()本身在實(shí)作上存在一些困難,尤其是當(dāng)物件本身又繼承另一個(gè)物件時(shí),如何確保複製的物件完整無誤,在不同的程式語言中有不同的作法。
在Java中的作法是透過實(shí)作一個(gè)Cloneable介面,它只是一個(gè)聲明的介面,並無規(guī)定任何實(shí)作的方法,您的目的是改寫Object的clone ()方法,使其具備有複製物件的功能,這個(gè)方面建議您參考:
How to avoid traps and correctly override methods from java.lang.Object。
用一個(gè)簡(jiǎn)單的例子來實(shí)作上圖中的結(jié)構(gòu),這個(gè)例子利用了Java語言本身的clone特性:
public abstract class AbstractFurniture
implements Cloneable {
public abstract void draw();
// 在Design Pattern上,以下的clone是抽象未實(shí)作的
// 實(shí)際上在Java中class都繼承自O(shè)bject
// 所以在這邊我們直接重新定義clone()
// 這是為了符合Java現(xiàn)行的clone機(jī)制
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
CircleTable與SquareTable繼承了AbstractFurniture,並實(shí)作clone方法,用於傳回本身的複製品:
import java.awt.*;
public class CircleTable extends AbstractFurniture {
protected Point center;
public void setCenter(Point center) {
this.center = center;
}
protected Object clone ()
throws CloneNotSupportedException {
Object o = super.clone();
if(this.center != null) {
((CircleTable) o).center = (Point) center.clone();
}
return o;
}
public void draw() {
System.out.println("\t圓桌\t中心:(" + center.getX()
+ ", " + center.getY()+ ")");
}
}
import java.awt.*;
public class SquareTable extends AbstractFurniture {
protected Rectangle rectangle;
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
protected Object clone ()
throws CloneNotSupportedException {
Object o = super.clone();
if(this.rectangle != null) {
((SquareTable) o).rectangle = (Rectangle) rectangle.clone();
}
return o;
}
public void draw() {
System.out.print("\t方桌\t位置:(" + rectangle.getX()
+ ", " + rectangle.getY()+ ")");
System.out.println(" / 寬高:(" +
rectangle.getWidth()
+ ", " + rectangle.getHeight()+ ")");
}
}
House是個(gè)虛擬的房屋物件,從Prototype複製出來的物件加入至House中:
import java.util.*;
public class House {
private Vector vector;
public House() {
vector = new Vector();
}
public void addFurniture(AbstractFurniture furniture) {
vector.addElement(furniture);
System.out.println("現(xiàn)有家具....");
Enumeration enumeration = vector.elements();
while(enumeration.hasMoreElements()) {
AbstractFurniture f =
(AbstractFurniture) enumeration.nextElement();
f.draw();
}
System.out.println();
}
}
再來是應(yīng)用程式本身:
import java.awt.*;
public class Application {
private AbstractFurniture circleTablePrototype;
public void setCircleTablePrototype(
AbstractFurniture circleTablePrototype) {
this.circleTablePrototype = circleTablePrototype;
}
public void runAppExample() throws Exception {
House house = new House();
CircleTable circleTable = null;
// 從工具列選擇一個(gè)家具加入房子中
circleTable =
(CircleTable) circleTablePrototype.clone();
circleTable.setCenter(new Point(10, 10));
house.addFurniture(circleTable);
// 從工具列選擇一個(gè)家具加入房子中
circleTable =
(CircleTable) circleTablePrototype.clone();
circleTable.setCenter(new Point(20, 30));
house.addFurniture(circleTable);
}
public static void main(String[] args) throws Exception {
Application application = new Application();
application.setCircleTablePrototype(
new CircleTable());
application.runAppExample();
}
}
Java中的clone()方法是繼承自O(shè)bject,AbstractFurniture的子類別則override這個(gè)clone()方法,以複製其本身並傳回。
下圖為Prototype模式的類別結(jié)構(gòu)圖:
在 Gof 的設(shè)計(jì)模式書中給出一個(gè)原型模式的應(yīng)用:一個(gè)通用的圖型編輯器 Framework。在這個(gè) Framework中有一個(gè)工具列,您可以在上面選擇音樂符號(hào)以加入樂譜中,並可以隨時(shí)調(diào)整音樂符號(hào)的位置等等。
圖型編輯器Framework是通用的,然而它並不知道這些音樂符號(hào)的型態(tài),有人或許會(huì)想到繼承圖型編輯器Framework來為每個(gè)音樂符號(hào)設(shè)計(jì)一個(gè)框架子類別,但由於音樂符號(hào)的種類很多,這會(huì)產(chǎn)生相當(dāng)多的子類別,為了避免這種情況,可以透過Prototype模式來減少子類別的數(shù)目,可以設(shè)計(jì)出以下的結(jié)構(gòu):
依照這個(gè)結(jié)構(gòu),圖型編輯器的Framework可以獨(dú)立於要套用的特定類別之外,雖然不知道被複製傳回的對(duì)象型態(tài)是什麼,但總可以按照 Graphics所定義的介面來操作這些物件。