Class Something
{
Public String Something=null;
}
Class Prototype implements Cloneable
{
private String name;
Public Something asomething=new Something ();
public void SetName (String name)
{
This.name=name;
}
Public String GetName ()
{
return name;
}
Public Prototype Clone ()
{
Prototype Object=null;
Try
{
object= (Prototype) Super.clone ();
}
catch (Clonenotsupportedexception exception)
{
System.err.println ("Can not cloneable");
}
return object;
}
}
public class Prototypepattern
{
public static void Main (string[] args)
{
Prototype aprototype=new Prototype ();
Aprototype.setname ("real One");
Aprototype.asomething.something= "real Something";
Prototype Acopyprototype=aprototype.clone ();
Acopyprototype.setname ("Clone One");
Aprototype.asomething.something= "Clone Something";
System.out.println ("Prototype:" +aprototype.getname () + ", Something:" +aprototype.asomething.something);
System.out.println ("Clone:" +acopyprototype.getname () + ", Something:" +acopyprototype.asomething.something);
}
}
18 of 23 Design Patterns in Java-prototype mode (Prototype pattern)