0
点赞
收藏
分享

微信扫一扫

设计模式(11) -- 原型模式


文章目录

  • ​​需求​​
  • ​​传统写法​​
  • ​​客户端测试Client​​
  • ​​优缺点:​​
  • ​​原型模式​​
  • ​​基本介绍​​
  • ​​UML类图​​
  • ​​原型模式解决决克隆羊问题​​
  • ​​原型模式在Spring框架中源码分析​​
  • ​​浅拷贝​​
  • ​​深拷贝​​
  • ​​重写clone方式​​
  • ​​通过对象的序列化实现 (推荐)​​
  • ​​原型模式的注意事项和细节​​
  • ​​源码地址​​


设计模式(11) -- 原型模式_ide

需求

现在有一只羊tom 姓 名为 : tom, 年龄为:1 颜 色为:白色,请编写程序创建和 tom羊属性完全相同的5只羊 。

传统写法

sheep

public class Sheep {
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
super();
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int 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 + "]";
}
}

客户端测试Client

public class Client {

public static void main(String[] args) {
// TODO Auto-generated method stub
//传统的方法
Sheep sheep = new Sheep("tom", 1, "白色");

Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
//....

System.out.println(sheep);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
}
}

运行结果:

设计模式(11) -- 原型模式_深拷贝_02

优缺点:

1)优点是比较好理解,简单易操作。
2)在 创建新的对 象 时 总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率较低
3)总是需要重新初始化对象,而不是动态地获得对象运行时的状态 , 不够灵活
4)改进的思路分析
​​思路​​
Java 中 Object 类是所有类的根类 Object 类提供了一个 clone() 方法,该方法可以将一 个 Java 对象复制一份 ,但是需要实现 clone Java 类必须要实现一个接口 Cloneable该接口表示该类能够复制且具有复制的能 力即**​​​原型模式​​**.

原型模式

基本介绍

设计模式(11) -- 原型模式_ide_03

UML类图

设计模式(11) -- 原型模式_原型模式_04


设计模式(11) -- 原型模式_ide_05

原型模式解决决克隆羊问题

设计模式(11) -- 原型模式_深拷贝_06


Sheep

public class Sheep implements Cloneable {
private String name;
private int age;
private String color;
private String address = "蒙古羊";
public Sheep friend; //是对象, 克隆是会如何处理

public Sheep(String name, int age, String color) {
super();
this.name = name;
this.age = age;
this.color = color;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int 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 + ", address=" + address + "]";
}

//克隆该实例,使用默认的clone方法来完成
@Override
protected Object clone() {

Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
// TODO Auto-generated method stub
return sheep;
}
}

Client

public class Client {
public static void main(String[] args) {
System.out.println("原型模式完成对象的创建");
// TODO Auto-generated method stub
Sheep sheep = new Sheep("tom", 1, "白色");

sheep.friend = new Sheep("jack", 2, "黑色");

Sheep sheep2 = (Sheep)sheep.clone(); //克隆
Sheep sheep3 = (Sheep)sheep.clone(); //克隆
Sheep sheep4 = (Sheep)sheep.clone(); //克隆
Sheep sheep5 = (Sheep)sheep.clone(); //克隆

System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode());
System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode());
System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode());
System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode());
}
}

运行结果:

设计模式(11) -- 原型模式_ide_07

原型模式在Spring框架中源码分析

设计模式(11) -- 原型模式_原型模式_08


如果我们配置的生成bean的方式是 property,追踪代码会看到,在第4步是通过原型模式来生成多个实例。

浅拷贝

设计模式(11) -- 原型模式_深拷贝_09


设计模式(11) -- 原型模式_深拷贝_10


设计模式(11) -- 原型模式_原型模式_11


设计模式(11) -- 原型模式_ide_12


这里的friend作为一个引用类型,其地址被每个实例化得到,如果一个实例改了friend,其他的实例也会受到影响,这就是浅拷贝,我们的目标只是想得到其全部属性就行了,而不是得到这个引用对象的地址。我们需要的是深拷贝。

深拷贝

设计模式(11) -- 原型模式_ide_13

引用对象成员 DeepCloneableTarget

public class DeepCloneableTarget  implements Serializable, Cloneable{
private static final long serialVersionUID = 1L;

private String cloneName;

private String cloneClass;

public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}

//因为该类的属性,都是String , 因此我们这里使用默认的clone完成即可
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}

@Override
public String toString() {
return "DeepCloneableTarget{" +
"cloneName='" + cloneName + '\'' +
", cloneClass='" + cloneClass + '\'' +
'}';
}
}

实现拷贝的类 DeepProtoType

public class DeepProtoType implements Serializable, Cloneable {
public String name; //String 属性
public DeepCloneableTarget deepCloneableTarget;// 引用类型
public DeepProtoType() {
super();
}


//深拷贝 - 方式 1 使用clone 方法
@Override
protected Object clone() throws CloneNotSupportedException {

Object deep = null;
//这里完成对基本数据类型(属性)和String的克隆
deep = super.clone();
//对引用类型的属性,进行单独处理
DeepProtoType deepProtoType = (DeepProtoType)deep;
deepProtoType.deepCloneableTarget = (DeepCloneableTarget)deepCloneableTarget.clone();

// TODO Auto-generated method stub
return deepProtoType;
}

//深拷贝 - 方式2 通过对象的序列化实现 (推荐)

public Object 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);
DeepProtoType copyObj = (DeepProtoType)ois.readObject();

return copyObj;

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
} finally {
//关闭流
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (Exception e2) {
// TODO: handle exception
System.out.println(e2.getMessage());
}
}

}

}

重写clone方式

设计模式(11) -- 原型模式_原型模式_14

通过对象的序列化实现 (推荐)

设计模式(11) -- 原型模式_ide_15


测试Client

public class Client {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DeepProtoType p = new DeepProtoType();
p.name = "宋江";
p.deepCloneableTarget = new DeepCloneableTarget("大牛", "小牛");

//方式1 完成深拷贝

DeepProtoType p1 = (DeepProtoType) p.clone();

System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode() +"=="+ p.deepCloneableTarget);
System.out.println("p1.name=" + p.name + "p1.deepCloneableTarget=" + p1.deepCloneableTarget.hashCode()+"=="+ p1.deepCloneableTarget);

//方式2 完成深拷贝
DeepProtoType p2 = (DeepProtoType) p.deepClone();

System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode()+"=="+ p.deepCloneableTarget);
System.out.println("p2.name=" + p.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode()+"=="+ p.deepCloneableTarget);

}
}

测试结果:

设计模式(11) -- 原型模式_深拷贝_16


可以看到hashcode 不一样,也就是引用地址不是一个,属性也深度克隆了。完美实现!!

原型模式的注意事项和细节

设计模式(11) -- 原型模式_设计模式_17

源码地址

​​https://github.com/hufanglei/java-design/tree/master/src/com/atguigu/prototype​​


举报

相关推荐

0 条评论