1 classroom exercise
2 code analysis
2.1 1 code analysis (prototype pattern prior to use)
2.2 code analysis 2
2.3 3 code analysis
Code analysis 4 2.4
5 2.5 code analysis
1 classroom exercise
2 code analysis
2.1 1 code analysis (prototype pattern prior to use)
demand:
Mail categories:
Package com.geely.design.pattern.creational.prototype; public class Mail { Private String name; Private String emailAddress; Private String Content; / ** * add initialization method. 1, while the purpose of cloning methods and out of the object, for Compared. * Check whether the cloned object is initialized this out. * / Public Mail () { System.out.println ( "the Constructor Class Mail" ); } / ** * 2 is easy to print, adding toString method * @return * / @Override public String toString () { return "Mail { "+ "name='" + name + '\'' + ", emailAddress='" + emailAddress + '\'' + ", content='" + content + '\'' + '}'; } /** * 3 以下是get和set方法 * @return */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
Mail Tools:
Package com.geely.design.pattern.creational.prototype; Import the java.text.MessageFormat; / ** * tools message, save the message and send the message to achieve the template initialization functions * / public class MailUtil { / ** * transmission. 1 mail function to achieve * key: placeholder character assignment to achieve * @param mail * / public static void the sendMail (Mail mail) { String sOutPutContent = "send mail content to students} {0} to {1, e-mail address: { } 2 " ; System.out.println (MessageFormat.format (sOutPutContent, mail.getName (), mail.getContent (), mail.getEmailAddress ())); } / ** * save log 2 * / public static void saveOriginMail (Mail mail) { System.out.println ( "content messages" + mail.getContent ()); } }
Test categories:
Package com.geely.design.pattern.creational.prototype; / ** * test class: * / public class the Test { / ** * Test. 1 (prior to cloning) using the prototype pattern * @param args * / public static void main ( String [] args) { mail mail = new new mail (); mail.setContent ( "template initialization" ); for ( int I = 0; I <10; I ++ ) { mail.setName ( "name" + I); mail .setContent ( "content" + I); mail.setEmailAddress (I+ "@ imooc.com" ); MailUtil.sendMail (mail); } MailUtil.saveOriginMail (mail); } }
Print log:
Mail Class Constructor send students to name 0 0 message content is content, e-mail address is: 0 @ imooc.com to name a classmate to send mail content is content 1, e-mail address is: 1 @ imooc.com send mail content to name 2 students 2 content, e-mail address is: 2 @ imooc.com to send students to name 3 3 message content is content, e-mail address is: 3 @ imooc.com send a message to the content name 4 4 students for the content, e-mail address is: 4 @ imooc.com send 5 students to name the contents of the message content to 5, e-mail address is: 5 @ imooc.com to name 6 students send message content as 6 content, e-mail address is: 6 @ imooc.com send a message to the content name 7 students 7 content, e-mail address is: 7 @ imooc.com to 8 names of the students send the message content is content 8, e-mail address is: 8 @ imooc.com to send students to name 9 9 mail content is content, e-mail address is: 9 @ imooc.com message content is content 9 Process Finished with Exit code 0
2.2 code analysis 2
Mail categories:
Package Penalty for com.geely.design.pattern.creational.prototype; / *
* only achieve clonable interface before calling the clone method
* / public class Mail the implements the Cloneable { Private String name; Private String emailAddress; Private String Content; / ** * 4 The method of adding shallow clone * @return * @throws CloneNotSupportedException * / @Override protected Object clone () throws CloneNotSupportedException { System.out.println ( "call cloning method"); return to super.clone (); } / ** * initialize method 1 was added, and while the purpose of cloning the object out, for comparison. * Check whether the cloned object is initialized this out. * / Public Mail () { System.out.println ( "the Constructor Class Mail" ); } / ** * 2 is easy to print, adding toString method
* add super.toString method for printing a memory address, compare the cloned target memory whether an address object and initializing the memory address consistent * and whether the memory address consistent between clone
* @return * / @Override public String toString () { return "Mail {" + "name = '" + name +' \ '' + ", emailAddress = '" + emailAddress +' \ '' + ", '}'; } /** * 3 以下是get和set方法 * @return */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
Mail Tools:
Package com.geely.design.pattern.creational.prototype; Import the java.text.MessageFormat; / ** * tools message, save the message and send the message to achieve the template initialization functions * / public class MailUtil { / ** * transmission. 1 mail function to achieve * key: placeholder character assignment to achieve * @param mail * / public static void the sendMail (Mail mail) { String sOutPutContent = "send mail content to students} {0} to {1, e-mail address: { } 2 " ; System.out.println (MessageFormat.format (sOutPutContent, mail.getName (), mail.getContent (), mail.getEmailAddress ())); } / ** * save log 2 * / public static void saveOriginMail (Mail mail) { System.out.println ( "content messages" mail.getContent + () + mail.getContent ()); } }
Test categories:
Package com.geely.design.pattern.creational.prototype; / ** * test class: * / public class the Test { / ** * Test. 1 (prior to cloning) using the prototype pattern * @param args * / public static void main ( String [] args) throws CloneNotSupportedException { mail mail = new new mail (); mail.setContent ( "template initialization" ); System.out.println ( "initialize the object memory address" mail +); for ( int I = 0; I <10; I ++ ) { Mail mailTemp = (Mail) mail.clone(); mailTemp.setName("姓名"+i); mailTemp.setContent("内容"+i); mailTemp.setEmailAddress(i+"@imooc.com"); MailUtil.sendMail(mailTemp); System.out.println("克隆对象"+i+"内存地址"+mailTemp); } MailUtil.saveOriginMail(mail); } }
Print log:
"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=3318:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes"com.geely.design.pattern.creational.prototype.Test Mail the Constructor Class initialize the memory address of the object name {Mail = 'null', emailAddress = 'null', Content = 'initialization template' } com.geely.design.pattern.creational @ .prototype.Mail 52a7b7ff call cloning send a message to the content name of content 0 0 classmates, e-mail address is: 0 @ imooc.com clone name {0 memory address Mail = 'name 0', emailAddress = '0 @ imooc. com ', content =' content 0 '} com.geely.design.pattern.creational.prototype.Mail @ 49,164,555 call cloning methods to send messages to names of the students content is content 1 1, e-mail address: 1 @ imooc.com clone {1 memory address name Mail = 'name 1', emailAddress='[email protected] ', content = ' content 1 ' } com.geely.design.pattern.creational.prototype.@ Mail 5521f4ef call cloning method Send a message to the content name of the content the students 2 2, e-mail address: 2 @ imooc.com clone name {2 memory address Mail = 'name 2', emailAddress='[email protected] ', content = ' content 2 ' }com.geely.design.pattern.creational.prototype.Mail@2857a293 call cloning send a message to the content name of the content 3 students 3, e-mail address: 3 @ imooc.com clone name {3 memory address Mail = 'name 3 ', emailAddress='[email protected]', content = ' content 3'}com.geely.design.pattern.creational.prototype.Mail@11727596 call cloning send a message to the content name of the content 4 4 students, the message address: 4 @ imooc.com clone name {4 memory address Mail = 'name 4', emailAddress='[email protected] ', content = ' content 4 ' } com.geely.design.pattern.creational.prototype .Mail @ 7185d3cc call cloning method to send mail content is content to name 5 5 students,E-mail address is: 5 @ imooc.com Clone 5 {name memory address Mail = 'name 5', emailAddress='[email protected] ', content = ' content 5 ' }com.geely.design.pattern.creational.prototype.Mail@914304e call cloning the name 6 students 6 E-mail content is content, e-mail address: 6 @ imooc.com clone {6 memory address name Mail = 'name 6', emailAddress='[email protected] ', content = ' content 6 ' } com.geely.design.pattern.creational.prototype.Mail@c6f558a call cloning send a message to the content name of the content students 7 7 mail address: 7 @ imooc.com clone 7 {name memory address Mail = 'name 7 ', emailAddress='[email protected]', content = 'content. 7' }com.geely.design.pattern.creational.prototype.Mail@112f8578 call cloning send a message to the content name of the content 8 8 students, e-mail address is: 8 @ imooc.com clone 8 memory address Mail {name= 'Name 8', emailAddress='[email protected] ', content = ' content. 8 ' }com.geely.design.pattern.creational.prototype.Mail@771c9fcc call cloning send a message to the content name of the content students 9 9, e-mail address is: 9 @ imooc.com clone 9 {name memory address Mail = 'name 9', emailAddress='[email protected] ', content = ' content 9 ' } com.geely.design.pattern. creational.prototype.Mail@4c0d39ac mail content templates for the initialization initialization template Process Finished with Exit code 0
2.3 3 code analysis
Code analysis 4 2.4
5 2.5 code analysis