原型模式
一、定义
Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象的实例。使用Prototype模式创建的实例,具有与原型一样的数据
二、原型模式特点
- 由原型对象自身创建目标对象,也就是说,对象创建这一动作发自原型对象(Person1)本身
- 目标对象是原型对象的一个克隆,也就是说,通过Prototype模式创建的对象,不仅仅与原型对象具有相同的结构,还与原型对象具有相同的值
- 根据对象克隆深度层次的不同,有浅度克隆与深度克隆
三、代码例子
3.1 Person
//需要实现Cloneable接口
public class Person implements Cloneable{
//属性
private String name;
private int age;
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
public Person clone(){
try {
return (Person)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
3.2 MainClass
public class MainClass {
public static void main(String[] args){
Person person1 = new Person();
person1.setName("Derrick");
person1.setAge(22);
person1.setSex("男");
/*
除了名字不同,其他都相同,则可使用原型模式
Person person2 = new Person();
person2.setName("Maomaos");
person2.setAge(22);
person2.setSex("男");
*/
//与Person person2 = person1;不同,下面那种方式地址是不同的,只是数据相同
Person person2 = person1.clone();
person2.setName("Maomaos");
//false
System.out.println(person1==person2);
System.out.println(person1.toString());
System.out.println(person2.toString());
}
}
3.3 运行结果
四、引用类型的属性只会复制引用地址(浅度克隆)
4.1 Person
//需要实现Cloneable接口
public class Person implements Cloneable{
//属性
private String name;
private int age;
private String sex;
//把引用复制一份
private List<String> friends;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public Person clone(){
try {
return (Person)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
4.2 MainClass
public class MainClass {
public static void main(String[] args){
Person person1 = new Person();
List<String> friends = new ArrayList<String>();
friends.add("Jessica1");
friends.add("Jessica2");
friends.add("Jessica3");
person1.setFriends(friends);
Person person2 = person1.clone();
System.out.println(person1.getFriends());
System.out.println(person2.getFriends());
friends.add("Jessica4");
person1.setFriends(friends);
//数据并不是每个人一份,引用一样的List的地址,所以数据一样
System.out.println(person1.getFriends());
System.out.println(person2.getFriends());
}
}
4.3 运行结果
4.4 图形说明
五、深度克隆
5.1 Person
//需要实现Cloneable接口
public class Person implements Cloneable{
//属性
private String name;
private int age;
private String sex;
//把引用复制一份
private List<String> friends;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public Person clone(){
try {
//数值型全相同
Person person = (Person)super.clone();
//引用类型重新赋值
List<String> friends = new ArrayList<String>();
for(String friend : this.getFriends()){
friends.add(friend);
}
person.setFriends(friends);
return person;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
5.2 MainClass
public class MainClass {
public static void main(String[] args){
Person person1 = new Person();
List<String> friends = new ArrayList<String>();
friends.add("Jessica1");
friends.add("Jessica2");
friends.add("Jessica3");
person1.setFriends(friends);
Person person2 = person1.clone();
System.out.println(person1.getFriends());
System.out.println(person2.getFriends());
friends.add("Jessica4");
person1.setFriends(friends);
//数据并不是每个人一份,引用一样的List的地址,所以数据一样
System.out.println(person1.getFriends());
System.out.println(person2.getFriends());
}
}