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"); 
  }
}
调用Prototype模式很简单:
程序代码:AbstractSpoon spoon = new SoupSpoon();
AbstractSpoon spoon2 = spoon.clone(); 

解决方案 »

  1.   

    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"); 
      }
    }
    调用Prototype模式很简单:
    程序代码:AbstractSpoon spoon = new SoupSpoon();
    AbstractSpoon spoon2 = spoon.clone();