一、介绍
原型模式是指用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象。原型模式是一中创建型模式,允许基于一个对象,创建拎一个可定制的对象,无需知道如何创建的细节。它的工作原理是通过将一个原型对象传给那个需要发动创建的对象,这个发动创建的对象通过请求原型对象,拷贝它们自己来实现创建,即对象克隆。
二、案例
以模拟克隆羊举例说明,假设现有一只羊,姓名tom,年龄1,颜色白,需要克隆跟它完全相同的10只
- 传统方式下,羊对象定义为:
/**
* 羊
*
* @author zhangxs
**/
public class Sheep {
private String name;
private int age;
private String color;
public Sheep(String name, int 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 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 + '\'' +
'}';
}
}
- 测试
/**
* 测试
*
* @author zhangxs
**/
public class Test {
public static void main(String[] args) {
Sheep sheep = new Sheep("Tom", 1, "白色");
Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
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());
Sheep sheep6 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep7 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep8 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep9 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep10 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
System.out.println(sheep1);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
System.out.println(sheep6);
System.out.println(sheep7);
System.out.println(sheep8);
System.out.println(sheep9);
System.out.println(sheep10);
}
}
传统方式一定是更好理解,操作起来更简单,但是在创建信息对象时,总是需要重新获取原始对象的属性,如果创建的对象较为复杂,则效率很低,另外,总是需要重新初始化对象,而不是动态的获取对象运行时的状态,不够灵活。基于上述情况,我们可以通过原型模式改进以往的方式,在Java中,Object类是所有类的根类,Object类中提供了一个clone()方法,该方法可以将一个Java对象复制一份,不过前提是需要该类实现Cloneable接口,该接口赋予对象可复制的能力
- 原型模式方式下,羊对象定义为:
/**
* 羊
*
* @author zhangxs
**/
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) {
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 + '\'' +
'}';
}
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return sheep;
}
}
- 测试
/**
* 测试
*
* @author zhangxs
**/
public class Test {
public static void main(String[] args) {
Sheep sheep = new Sheep("Tom", 1, "白色");
sheep.friend = new Sheep("Jim", 3, "黑色");
Sheep sheep1 = (Sheep) sheep.clone();
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
Sheep sheep4 = (Sheep) sheep.clone();
Sheep sheep5 = (Sheep) sheep.clone();
System.out.println("sheep1=" + sheep1 + "sheep1.friend=" + sheep1.friend.hashCode());
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());
}
}
三、总结
- 优点
1、不用重新初始化对象,而是动态获得对象运行时的状态。
2、如果原始对象发生变化(如:增加或减少属性),其他克隆对象也会跟着发生相应的变化,无需修改代码。 - 缺点
需要为每一个类配备一个克隆方法,对于全新的类并不是大问题,但对于原有的类进行改在,需要修改其源代码,违背了开闭原则。 - 适用场景
创建的新对象较为复杂时,可以利用原型模式简化对象的创建过程,同时也能提高对象创建的效率。