1.简介
- 原型模式(Prototype模式):
- 通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。
- 就是java中的克隆技术,以某个对象为原型,夫指出新的对象。显然,新的对象具备原型对象的特点
- 优势有:效率高(直接克隆,避免了重新执行构造过程步骤)
- 克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的对象的属性值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后再修改克隆对象的值。
- 原型模式实现:
Cloneable
接口和clone方法- Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在java中提供了clone()方法替我们做了绝大部分事情
- 注意用词:克隆和拷贝是一回事
2. 代码测试
创建Sheep
类
/**
* 克隆羊多利
*/
public class Sheep implements Cloneable {
private String name;
private Date birthday;
/**
* 重写Object的clone方法
*/
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();// 直接调用Object的clone方法
return obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
@Override
public String toString() {
return "Sheep [name=" + name + ", birthday=" + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + "]";
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Sheep() {
super();
}
}
测试类
/**
* 测试原型模式(浅克隆)
*/
public class Client {
public static void main(String[] args) throws CloneNotSupportedException, ParseException {
Sheep s1 = new Sheep("Tom", new SimpleDateFormat("yyyy-MM-dd").parse("1998-01-01"));
System.out.println("s1-->" + s1);// s1-->Sheep [name=Tom, birthday=1998-01-01]
// 调用s1的clone方法
Sheep s2 = (Sheep) s1.clone();
System.out.println("s2-->" + s2);// s2-->Sheep [name=Tom, birthday=1998-01-01]
System.out.println("s1 和 s2 的birthday属性是否为同一对象:" + (s1.getBirthday() == s2.getBirthday()));// s1 和 s2 的birthday属性是否为同一对象:true
// 更改s1的birthday属性值
s1.getBirthday().setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2012-10-10").getTime());
System.out.println("更改s1的birthday属性值之后");
System.out.println("s1-->" + s1);// s1-->Sheep [name=Tom, birthday=2012-10-10]
System.out.println("s2-->" + s2);// s2-->Sheep [name=Tom, birthday=2012-10-10]
// 验证s1、s2是否为同一对象
System.out.println(s1 == s2);// false
}
}
控制台打印
s1-->Sheep [name=Tom, birthday=1998-01-01]
s2-->Sheep [name=Tom, birthday=1998-01-01]
s1 和 s2 的birthday属性是否为同一对象:true
更改s1的birthday属性值之后
s1-->Sheep [name=Tom, birthday=2012-10-10]
s2-->Sheep [name=Tom, birthday=2012-10-10]
false
浅克隆和深克隆
修改Sheep
类
/**
* 克隆羊多利
*/
public class Sheep implements Cloneable {
private String name;
private Date birthday;
/**
* 重写Object的clone方法
*/
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();// 直接调用Object的clone方法
// -----------------------
// 添加如下代码实现深克隆
Sheep s = (Sheep) obj;
s.birthday = (Date) birthday.clone();// 克隆属性
// -------------------
return obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
@Override
public String toString() {
return "Sheep [name=" + name + ", birthday=" + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + "]";
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Sheep() {
super();
}
}
测试深克隆
/**
* 原型模式(深克隆)
*/
public class Client2 {
public static void main(String[] args) throws CloneNotSupportedException, ParseException {
Sheep s1 = new Sheep("Tom", new SimpleDateFormat("yyyy-MM-dd").parse("1998-01-01"));
System.out.println("s1-->" + s1);// s1-->Sheep [name=Tom, birthday=1998-01-01]
// 调用s1的clone方法
Sheep s2 = (Sheep) s1.clone();
System.out.println("s2-->" + s2);// s2-->Sheep [name=Tom, birthday=1998-01-01]
System.out.println("s1 和 s2 的birthday属性是否为同一对象:" + (s1.getBirthday() == s2.getBirthday()));// s1 和 s2 的birthday属性是否为同一对象:false
// 更改s1的birthday属性值
s1.getBirthday().setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2012-10-10").getTime());
System.out.println("更改s1的birthday属性值之后");
System.out.println("s1-->" + s1);// s1-->Sheep [name=Tom, birthday=2012-10-10]
System.out.println("s2-->" + s2);// s2-->Sheep [name=Tom, birthday=1998-01-01]
// 验证s1、s2是否为同一对象
System.out.println(s1 == s2);// false
}
}
控制台打印
s1-->Sheep [name=Tom, birthday=1998-01-01]
s2-->Sheep [name=Tom, birthday=1998-01-01]
s1 和 s2 的birthday属性是否为同一对象:false
更改s1的birthday属性值之后
s1-->Sheep [name=Tom, birthday=2012-10-10]
s2-->Sheep [name=Tom, birthday=1998-01-01]
false
3.使用序列化和反序列化实现深克隆
前提:Sheep
类实现Serializable
接口
代码实现
/**
* 使用序列化和反序列化实现深克隆
*/
public class Client3 {
public static void main(String[] args) throws ParseException, IOException, ClassNotFoundException {
Sheep s1 = new Sheep("Tom", new SimpleDateFormat("yyyy-MM-dd").parse("1998-01-01"));
System.out.println("s1-->" + s1);// s1-->Sheep [name=Tom, birthday=1998-01-01]
// 使用序列化和反序列化实现深克隆
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s1);
byte[] byteArray = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
ObjectInputStream ois = new ObjectInputStream(bais);
Sheep s2 = (Sheep) ois.readObject();// 实现深克隆
System.out.println("s2-->" + s2);// s2-->Sheep [name=Tom, birthday=1998-01-01]
System.out.println("s1 和 s2 的birthday属性是否为同一对象:" + (s1.getBirthday() == s2.getBirthday()));// s1 和 s2 的birthday属性是否为同一对象:false
// 更改s1的birthday属性值
s1.getBirthday().setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2012-10-10").getTime());
System.out.println("更改s1的birthday属性值之后");
System.out.println("s1-->" + s1);// s1-->Sheep [name=Tom, birthday=2012-10-10]
System.out.println("s2-->" + s2);// s2-->Sheep [name=Tom, birthday=1998-01-01]
// 验证s1、s2是否为同一对象
System.out.println(s1 == s2);// false
}
}
4. 开发中的应用场景
- 原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。
- spring中bean的创建实际就是两种:单例模式和原型模式。(当然,原型模式需要和工厂模式搭配起来)