《Android源码设计模式解析和实战》原型模式
原型模式方便吧之前在对象中设置好的值直接拿过来用或者修改;
强拷贝 在拷贝的过程中遇到对象再进行拷贝,不再引用被拷贝对象中的对象属性。
浅拷贝 在拷贝过程中遇到对象直接再次引用;
对对象中的对象属性进行修改时 强拷贝不会修改 拷贝对象的属性;浅拷贝则会;
<pre>
ArrayList<Integer> numberList = new ArrayList<>();
numberList.add(1);
numberList.add(2);
Teacher teacher_1 = new Teacher(person_3);
teacher_1.setNumberList(numberList);
Teacher teacher_2 = (Teacher) teacher_1.clone();
numberList.add(3);
</pre>
当 Teacher.clone()中为强饮用时 teacher_1会改变 teacher_2 不会
当 Teacher.clone()中为浅饮用时 teacher_1会改变 teacher_2 也会
实体类:
<pre>
public class Person implements Cloneable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
protected Object clone() {
Person person = null;
try {
person = (Person) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return person;
}
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Person{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append('}');
return sb.toString();
}
}
public class Student extends Person {
private int number;
public Student(int number) {
this.number = number;
}
public Student(Person person) {
setName(person.getName());
setAge(person.getAge());
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Student{");
sb.append("number=").append(number);
sb.append('}');
return sb.toString();
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
protected Object clone() {
Student student = null;
student = (Student) super.clone();
student.number = number;
return student;
}
}
public class Teacher extends Person {
private ArrayList<Integer> numberList;
public Teacher() {
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Teacher{");
sb.append("numberList=").append(numberList);
sb.append('}');
return sb.toString();
}
public Teacher(Person person) {
setName(person.getName());
setAge(person.getAge());
}
public ArrayList<Integer> getNumberList() {
return numberList;
}
public void setNumberList(ArrayList<Integer> numberList) {
this.numberList = numberList;
}
@Override
protected Object clone() {
Teacher teacher = (Teacher) super.clone();
// teacher.numberList = (ArrayList<Integer>) numberList.clone();
teacher.numberList =numberList;
return teacher;
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Person> personArrayList = new ArrayList<>();
Person person_1 = new Person("张三", 10);
Person person_2 = new Person("李斯", 10);
Person person_3 = new Person("张老师", 10);
Student student_1 = new Student(person_1);
student_1.setNumber(1);
personArrayList.add(student_1);
Student student_2 = new Student(person_2);
student_2.setNumber(2);
personArrayList.add(student_2);
ArrayList<Integer> numberList = new ArrayList<>();
numberList.add(student_1.getNumber());
numberList.add(student_2.getNumber());
Teacher teacher_1 = new Teacher(person_3);
teacher_1.setNumberList(numberList);
personArrayList.add(teacher_1);
Teacher teacher_2 = (Teacher) teacher_1.clone();
numberList.add(3);
personArrayList.add(teacher_1);
personArrayList.add(teacher_2);
for (Person person : personArrayList) {
Log.i(TAG, "onCreate: " + person.toString());
}
}
}
teacher.numberList = (ArrayList<Integer>) numberList.clone(); //强拷贝
03-01 20:39:08.798 15321-15321/com.lerz.clonedemo I/MainActivity: onCreate: Student{number=1}
03-01 20:39:08.798 15321-15321/com.lerz.clonedemo I/MainActivity: onCreate: Student{number=2}
03-01 20:39:08.798 15321-15321/com.lerz.clonedemo I/MainActivity: onCreate: Teacher{numberList=[1, 2, 3]}
03-01 20:39:08.798 15321-15321/com.lerz.clonedemo I/MainActivity: onCreate: Teacher{numberList=[1, 2, 3]}
03-01 20:39:08.798 15321-15321/com.lerz.clonedemo I/MainActivity: onCreate: Teacher{numberList=[1, 2]}
teacher.numberList =numberList;//浅拷贝
03-01 20:34:23.558 13827-13827/com.lerz.clonedemo I/MainActivity: onCreate: Student{number=1}
03-01 20:34:23.558 13827-13827/com.lerz.clonedemo I/MainActivity: onCreate: Student{number=2}
03-01 20:34:23.558 13827-13827/com.lerz.clonedemo I/MainActivity: onCreate: Teacher{numberList=[1, 2, 3]}
03-01 20:34:23.558 13827-13827/com.lerz.clonedemo I/MainActivity: onCreate: Teacher{numberList=[1, 2, 3]}
03-01 20:34:23.558 13827-13827/com.lerz.clonedemo I/MainActivity: onCreate: Teacher{numberList=[1, 2, 3]}
</pre>