概述
- 通过原型对象,复制(拷贝)一个与原型对象相同的新对象。
- 其实就是拷贝对象。可分为 浅拷贝(对象内的对象属性与原型对象地址引用一样) 和 深拷贝(对象内的对象属性是拷贝的新对象)
使用场景
- 在对象特别复杂时,或者有性能或安全问题时,可以考虑使用原型模式
浅拷贝
- Java中Object类的clone()方法实现了浅拷贝
- 实现Cloneable,并重写clone()方法,即可进行浅拷贝
实现
public class Test1 {
public static void main(String[] args) throws CloneNotSupportedException {
Duck duck = new Duck();
duck.setName("duck");
Duck clone = duck.clone();
clone.setName("clone");
System.out.println(duck == clone);
duck.show();
clone.show();
BlackDuck blackDuck = new BlackDuck();
duck.setBlackDuck(blackDuck);
blackDuck.setName("duck");
clone.setBlackDuck(blackDuck);
clone.getBlackDuck().setName("clone");
duck.say();
clone.say();
}
}
class Duck implements Cloneable{
private String name;
public Duck() {
System.out.println("clone没有调用构造函数");
}
@Override
protected Duck clone() throws CloneNotSupportedException {
return (Duck) super.clone();
}
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.println(name);
}
private BlackDuck blackDuck;
public void say() {
System.out.println(blackDuck.getName());
}
public void setBlackDuck(BlackDuck blackDuck) {
this.blackDuck = blackDuck;
}
public BlackDuck getBlackDuck() {
return blackDuck;
}
}
class BlackDuck {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
深拷贝
实现
public class Test {
public static void main(String[] args) throws Exception {
Duck duck = new Duck();
BlackDuck blackDuck = new BlackDuck();
blackDuck.setName("duck");
duck.setBlackDuck(blackDuck);
write(duck);
Duck clone = (Duck) read();
clone.getBlackDuck().setName("clone");
duck.say();
clone.say();
}
private static void write(Object o) throws Exception {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\work\\study\\design\\single.txt"));
out.writeObject(o);
out.close();
}
private static Object read() throws Exception {
ObjectInputStream input = new ObjectInputStream(new FileInputStream("D:\\work\\study\\design\\single.txt"));
Object o = input.readObject();
return o;
}
}
class Duck implements Serializable {
private BlackDuck blackDuck;
public void say() {
System.out.println(blackDuck.getName());
}
public void setBlackDuck(BlackDuck blackDuck) {
this.blackDuck = blackDuck;
}
public BlackDuck getBlackDuck() {
return blackDuck;
}
}
class BlackDuck implements Serializable {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}