原型模式的目標是通過降低創建成本來克隆對象。 這裏是一個例子:如何實現原型模式?
class Complex {
int[] nums = {1,2,3,4,5};
public Complex clone() {
return new Complex();//this line create a new object, so is it violate the objective of prototype ?//
}
}
class Test2 {
Complex c1 = new Complex();
Complex makeCopy() {
return (Complex)c1.clone();// Is it actually create a new object ? based on the clone method in Complex class? //
}
public static void main(String[] args) {
Test2 tp = new Test2();
Complex c2 = tp.makeCopy();
}
}
我認爲這是深層複製。那麼,有人可以幫助我解決這個問題嗎?
我也查了維基百科對這種模式的定義。我同意這是沒有意義的(類正在被實例化),除非你利用對象緩存。 – 2010-08-20 16:57:32