求教:模型设计之Prototype

1.Prototype模型怎么用啊?
2.谁有模型设计的好书,介绍一本,band 先生的我己经有一份了,就是我有的那份写的不太细,举例有点少,对于初学者,我有点理解不好
xsj4318@yahoo.com.cn我的电邮,
发网站给我也行
thank you for your help _wrriten by super xsj@

Prototype模型很难理解,其实和大家对对象实例的运行形式不太理解有关,目前有两种:单例和多例,Prototype模型是产生多例的一个创建方式,当然普通new也是产生多例方式,Prototype区别就是象Java中的clone,克隆,将原来对象状态复制一份出来。

调用Prototype模式很简单:

AbstractSpoon spoon = new SoupSpoon();
AbstractSpoon spoon = new SaladSpoon();

band 先生,这是你的文件里关于prototype的讲述,但是,
1。我没有看到调用clone方法呀?这是为什么?
2。对于shadow clone的问题,我是不是还得用深度解决,还是直接不让shadow clone问题出现?

附:1。对不起,由于着急等答案,将名字写错
2。以下是更多的代码,可能为了你解决方便,给你打出来

以勺子为例:

public abstract class AbstractSpoon implements Cloneable
{
  String spoonName;

  public void setSpoonName(String spoonName) {this.spoonName = spoonName;}
  public String getSpoonName() {return this.spoonName;}

  public Object clone()
  {
    Object object = null;
    try {
      object = super.clone();
    } catch (CloneNotSupportedException exception) {
      System.err.println("AbstractSpoon is not Cloneable");
    }
    return object;
  }
}

有两个具体实现(ConcretePrototype):

public class SoupSpoon extends AbstractSpoon
{
  public SoupSpoon()
  {
    setSpoonName("Soup Spoon");
  }
}

public class SaladSpoon extends AbstractSpoon
{
  public SaladSpoon()
  {
    setSpoonName("Salad Spoon");
  }
}

这个模式不难,就是用现在的对象再复制一份出来?
具体是使用Object.clone()方法

给你一个例子


/*
* @(#)Test2.java 1.1 2006-11-7
*
* Copyright 2006 Moloon, Inc. All rights reserved.
* MOLOON PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package com.create.prototype;

/**
* @author lvyg
* @version 1.1, 2006-11-7
* @since JDK1.5
*/

public class Test2 implements Cloneable {
private String test =
"";

public String test() {
return test;
}

public Object clone() {
Object obj = null;

try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Test2 test = new Test2();
test.test =
"123";
System.out.println(
"test: " + test.test());
Test2 test2 = (Test2) test.clone();
System.out.println(
"test2: " + test2.test());
}

}