public class AluminiumKey extends KeyPrototype{
}
---------------------------------
public class CopperKey extends KeyPrototype{
}
----------------------
/**
* 配钥匙的例子
* @author db2admin
*
*/
public abstract class KeyPrototype implements Cloneable{
private String color;
private float length;
private float thick;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return this.getClass() + " -> Color:" + this.getColor() + " Length:"
+ this.getLength() + " Thick:" + this.getThick();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getLength() {
return length;
}
public void setLength(float length) {
this.length = length;
}
public float getThick() {
return thick;
}
public void setThick(float thick) {
this.thick = thick;
}
}
------------------------------------
package 原型模式;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
KeyPrototype copperkey = new CopperKey();
copperkey.setColor("red");
copperkey.setLength(12);
copperkey.setThick(2);
System.out.println(copperkey);
try {
KeyPrototype aluminaumkey = (KeyPrototype) copperkey.clone();
aluminaumkey.setColor("yellow");
aluminaumkey.setLength(10);
aluminaumkey.setThick(5);
System.out.println(aluminaumkey);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}