笔记来源:尚硅谷Java设计模式(图解+框架源码剖析)
现在有一只羊,姓名为 Tom,年龄为 1,颜色为白色,请编写程序创建和 Tom 羊属性完全相同的 10 只羊
传统方法
public class Sheep {
private String name;
private Integer age;
public Sheep(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
public class Client {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Sheep sheep = new Sheep("Tom", 1, "白色");
System.out.println(sheep);
}
}
}
传统方法优缺点
改进的思路分析
Java 中 Object 类是所有类的根类,Object 类提供了一个 clone 方法,该方法可以将一个 Java 对象复制一份,但是需要实现 clone 的 Java 类必须要实现一个接口 Cloneable,该接口表示该类能够复制且具有复制的能力 ==> 原型模式
1)原型模式(Prototype 模式)是指:用原型实例指定创建对象种类,并通过拷贝原型创建新的对象
2)原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节
3)工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即对象.clone()
4)形象的理解:孙大圣拔出猴毛,变出其它孙大圣
原理结构图说明
Prototype
:原型类,声明一个克隆自己的接口ConcretePrototype
:具体的原型类,实现一个克隆自己的操作Client
:让一个原型对象克隆自己,创建一个新的对象(属性相同)使用原型模式改进传统方式式,让程序具有更高的效率和扩展性
UML 类图
核心代码
public class Sheep implements Cloneable {
private String name;
private Integer age;
private String color;
public Sheep(String name, Integer age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
} catch (Exception e) {
e.printStackTrace();
}
return sheep;
}
}
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("Tom", 1, "白色");
for (int i = 0; i < 10; i++) {
Sheep sheep1 = (Sheep) sheep.clone();
System.out.println(sheep1);
}
}
}
Spring 框架中,创建ApplicationContext
时,使用的getBean
方法中使用到了原型模式
sheep=(Sheep)super.clone();
深拷贝方式 1
public class DeepClonableTarget implements Serializable, Cloneable {
private String cloneName;
private String cloneClass;
public DeepClonableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
public String getCloneName() {
return cloneName;
}
public void setCloneName(String cloneName) {
this.cloneName = cloneName;
}
public String getCloneClass() {
return cloneClass;
}
public void setCloneClass(String cloneClass) {
this.cloneClass = cloneClass;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class DeepPrototype implements Serializable, Cloneable {
private String name;
private DeepClonableTarget deepClonableTarget;
public DeepPrototype() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DeepClonableTarget getDeepClonableTarget() {
return deepClonableTarget;
}
public void setDeepClonableTarget(DeepClonableTarget deepClonableTarget) {
this.deepClonableTarget = deepClonableTarget;
}
@Override
protected Object clone() throws CloneNotSupportedException {
//基本数据类型拷贝
Object object = super.clone();
//引用类型拷贝
DeepPrototype deepPrototype = (DeepPrototype) object;
deepPrototype.deepClonableTarget = (DeepClonableTarget) deepClonableTarget.clone();
return object;
}
}
public class DeepTest {
public static void main(String[] args) throws CloneNotSupportedException {
DeepPrototype prototype = new DeepPrototype();
prototype.setName("宋江");
prototype.setDeepClonableTarget(new DeepClonableTarget("及时雨", "及时雨的类"));
DeepPrototype clone1 = (DeepPrototype) prototype.clone();
DeepPrototype clone2 = (DeepPrototype) prototype.clone();
DeepPrototype clone3 = (DeepPrototype) prototype.clone();
DeepPrototype clone4 = (DeepPrototype) prototype.clone();
DeepPrototype clone5 = (DeepPrototype) prototype.clone();
System.out.println(prototype.getName() + ", " + prototype.getDeepClonableTarget().hashCode()); // 宋江, 1554874502
System.out.println(clone1.getName() + ", " + clone1.getDeepClonableTarget().hashCode()); // 宋江, 1846274136
System.out.println(clone2.getName() + ", " + clone2.getDeepClonableTarget().hashCode()); // 宋江, 1639705018
System.out.println(clone3.getName() + ", " + clone3.getDeepClonableTarget().hashCode()); // 宋江, 1627674070
System.out.println(clone4.getName() + ", " + clone4.getDeepClonableTarget().hashCode()); // 宋江, 1360875712
System.out.println(clone5.getName() + ", " + clone5.getDeepClonableTarget().hashCode()); // 宋江, 1625635731
}
}
深拷贝方式 2
public class DeepClonableTarget implements Serializable, Cloneable {
private String cloneName;
private String cloneClass;
public DeepClonableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
public String getCloneName() {
return cloneName;
}
public void setCloneName(String cloneName) {
this.cloneName = cloneName;
}
public String getCloneClass() {
return cloneClass;
}
public void setCloneClass(String cloneClass) {
this.cloneClass = cloneClass;
}
}
public class DeepPrototype implements Serializable, Cloneable {
private String name;
private DeepClonableTarget deepClonableTarget;
public DeepPrototype() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DeepClonableTarget getDeepClonableTarget() {
return deepClonableTarget;
}
public void setDeepClonableTarget(DeepClonableTarget deepClonableTarget) {
this.deepClonableTarget = deepClonableTarget;
}
public DeepPrototype deepClone() {
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
// 序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
// 反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
return (DeepPrototype) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (ois != null) {
ois.close();
}
if (bis != null) {
bis.close();
}
if (oos != null) {
oos.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class DeepTest {
public static void main(String[] args) throws CloneNotSupportedException {
DeepPrototype prototype = new DeepPrototype();
prototype.setName("宋江");
prototype.setDeepClonableTarget(new DeepClonableTarget("及时雨", "及时雨的类"));
DeepPrototype clone1 = prototype.deepClone();
DeepPrototype clone2 = prototype.deepClone();
DeepPrototype clone3 = prototype.deepClone();
DeepPrototype clone4 = prototype.deepClone();
DeepPrototype clone5 = prototype.deepClone();
System.out.println(prototype.getName() + ", " + prototype.getDeepClonableTarget().hashCode()); // 宋江, 644117698
System.out.println(clone1.getName() + ", " + clone1.getDeepClonableTarget().hashCode()); // 宋江, 317574433
System.out.println(clone2.getName() + ", " + clone2.getDeepClonableTarget().hashCode()); // 宋江, 885284298
System.out.println(clone3.getName() + ", " + clone3.getDeepClonableTarget().hashCode()); // 宋江, 1389133897
System.out.println(clone4.getName() + ", " + clone4.getDeepClonableTarget().hashCode()); // 宋江, 1534030866
System.out.println(clone5.getName() + ", " + clone5.getDeepClonableTarget().hashCode()); // 宋江, 664223387
}
}
方式 1 和方式 2 对比