Article Directory
Outline
Definition: Prototype instance of the specified type to create an object, and create new objects by copying the prototype
Features: Do not need to know the details of any creation, do not call the constructor
Type: Creating type
Applicable scene
Class initialization consume more resources
a new object is created requires a very tedious process (data preparation, access
restriction, etc.)
Constructor is more complex
When a large number of objects produced in the loop
advantage
Prototype model performance than direct a new target performance
Simplify the creation of
Shortcoming
Must be equipped cloning method (clone)
When cloning complex objects or complex transformation of objects cloned, easy to introduce risk
Deep copy and shallow copy to be used properly
Expanding
deep clone
shallow clone
Show
Send bulk mail,
create a class Mail
Remember to inherit cloneable
package prototype;
public class Mail implements Cloneable {
private String name;
private String emailAddress;
private String content;
public Mail() {
System.out.println("Mail Class Construstor");
}
public String getName() {
return name;
}
public String getEmailAddress() {
return emailAddress;
}
public String getContent() {
return content;
}
public void setName(String name) {
this.name = name;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Mail{" +
"name='" + name + '\'' +
", emailAddress='" + emailAddress + '\'' +
", content='" + content + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
System.out.println("clone mail object");
return super.clone();
}
}
Creating Tools
package prototype;
import java.text.MessageFormat;
public class MainUtil {
public static void sendMain(Mail mail){
String outputContent = "向亲爱的{0},邮件地址:{1},邮件内容:{2}发送邮件成功";
System.out.println(MessageFormat.format(outputContent,mail.getName(),mail.getEmailAddress(),mail.getContent()));
}
public static void saveOriginMailRecord(Mail mail){
System.out.println("存储originMail记录originMail:" +mail.getContent());
}
}
Test categories:
package prototype;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Mail mail = new Mail();
mail.setContent("初始化模板");
for (int i = 0; i <10 ; i++) {
Mail mailTemp = (Mail) mail.clone(); //二进制流的copy速度比new快
mailTemp.setName("顾客"+i);
mailTemp.setEmailAddress("姓名"+i+"@163.com");
mailTemp.setContent("恭喜你中了1000万大奖,");
MainUtil.sendMain(mailTemp);
}
MainUtil.saveOriginMailRecord(mail);
}
}
result:
If the abstract service logic can be used for the case where an abstract
example A
package prototype;
public class A implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
B inherits A
package prototype;
public class B extends A{
public static void main(String[] args) throws CloneNotSupportedException {
B b = new B();
b.clone();
}
}
Method B also inherited clone A and clone B can be achieved
Prototype model also has a number of pits, if they do not know him very susceptible to things
introduced deep and shallow clone clone below
to create a Pig class
Object Date concern Pig class
package prototype;
import java.util.Date;
public class Pig implements Cloneable{
private String name;
private Date birthday;
public Pig(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Pig{" +
"name='" + name + '\'' +
", birthday=" + birthday +
'}'+super.toString();
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
The following test
package prototype;
import java.util.Date;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Date birthday = new Date(0L);
Pig pig1 = new Pig("佩奇",birthday);
Pig pig2 = (Pig) pig1.clone();
System.out.println(pig1);
System.out.println(pig2);
pig1.getBirthday().setTime(6666666L);
System.out.println(pig1);
System.out.println(pig2);
}
}
Print result:
you will find only modify the date of birth pig1 here but pig2 date of birth also followed changed
Debugging it is easy to find
birthday is the same object
This is a shallow clone
Solution is simple reflects a deep clone
Wherein the core code rewrite clone method code cPig
@Override
protected Object clone() throws CloneNotSupportedException {
Pig pig = (Pig)super.clone();
//深克隆
pig.birthday = (Date) pig.birthday.clone();
return pig;
}
Run again:
we must pay attention to this problem ah, otherwise it will lead to bug