1. The scenario is resolved
1.1 Scenario Description
Problems encountered in the e-bill project: e-bill and advertisement letter of the bank: Features: large quantity and tight time requirement. After the e-bill content is generated, it can be sent successfully quickly.
1.2 OO design
Multithreaded implementation
- EventTemplate is a public object
public class EventTemplate { private String eventSubject, eventContent; public EventTemplate(String eventSubject, String eventContent) { this.eventSubject = eventSubject; this.eventContent = eventContent; } public String geteventSubject() { return eventSubject; } public String geteventContent() { return eventContent; }}Copy the code
- Oo is designed for OO
-
- Mail Mail object
-
- MainOOTest test class
public class Mail { private String receiver; private String subject; private String content; private String tail; public Mail(EventTemplate et) { this.tail = et.geteventContent(); this.subject = et.geteventSubject(); } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public void setContent(String content) { this.content = content; } public String getContent() { return content; } public String getTail() { return tail; } public void setTail(String tail) { this.tail = tail; } } public class MainOOTest { public static void main(String[] args) { int i = 0; int MAX_COUNT = 10; EventTemplate et = new EventTemplate(" September credit card bill ", "National Day raffle...") ); Mail mail = new Mail(et); SetContent (getRandString(5) + ", Sir (Madam) : Your credit card bill..." + mail.getTail()); mail.setReceiver(getRandString(5) + "@" + getRandString(8) + ".com"); // Then send the mail sendMail(mail); i++; } } public static String getRandString(int maxLength) { String source = "abcdefghijklmnopqrskuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuffer sb = new StringBuffer(); Random rand = new Random(); for (int i = 0; i < maxLength; i++) { sb.append(source.charAt(rand.nextInt(source.length()))); } return sb.toString(); } public static void sendMail(Mail Mail) {system.out.println (" header: "+ mail.getSubject() + "\t Recipient: "+ mail. GetReceiver () +" \ t content: "+ mail. GetContent () +" \ t... Sent successfully!" ); }}Copy the code
1.3 Demand Change
1.4 Bringing Problems
2. Improve with design patterns
2.1 analysis
2.2 Redesign
2.3 the source code
- EventTemplate is a public object
public class EventTemplate { private String eventSubject, eventContent; public EventTemplate(String eventSubject, String eventContent) { this.eventSubject = eventSubject; this.eventContent = eventContent; } public String geteventSubject() { return eventSubject; } public String geteventContent() { return eventContent; }}Copy the code
- Proto is a prototype design pattern
-
- Mail Mail object
-
- MainProtoTest test class
public class Mail implements Cloneable { private String receiver; private String subject; private String content; private String tail; Private ArrayList<String> ArrayList; public Mail(EventTemplate et) { this.tail = et.geteventContent(); this.subject = et.geteventSubject(); } @Override public Mail clone() { Mail mail = null; try { mail = (Mail) super.clone(); Mail.arraylist = (arrayList <String>)this.arrayList.clone(); mail.arrayList = (arrayList <String>)this.arrayList.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return mail; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public void setContent(String content) { this.content = content; } public String getContent() { return content; } public String getTail() { return tail; } public void setTail(String tail) { this.tail = tail; } } public class MainProtoTest { public static void main(String[] args) { int i = 0; int MAX_COUNT = 10; EventTemplate et = new EventTemplate(" September credit card bill ", "National Day raffle...") ); Mail mail = new Mail(et); While (I < MAX_COUNT) {cloneMail = mail.clone(); Clonemail.setcontent (getRandString(5) + ", Sir (Madam) : Your credit card bill..." + mail.getTail()); cloneMail.setReceiver(getRandString(5) + "@" + getRandString(8) + ".com"); // Then send the mail sendMail(cloneMail); i++; } } public static String getRandString(int maxLength) { String source = "abcdefghijklmnopqrskuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuffer sb = new StringBuffer(); Random rand = new Random(); for (int i = 0; i < maxLength; i++) { sb.append(source.charAt(rand.nextInt(source.length()))); } return sb.toString(); } public static void sendMail(Mail Mail) {system.out.println (" header: "+ mail.getSubject() + "\t Recipient: "+ mail. GetReceiver () +" \ t content: "+ mail. GetContent () +" \ t... Sent successfully!" ); }}Copy the code
3. Summary of design patterns
3.1 define
Prototype pattern: Create a new instance by copying an existing instance without knowing about the corresponding class
3.2 Analysis
3.3 the advantages and disadvantages
- Advantages: Using the stereotype pattern to create objects is more efficient than simply new an object and hides the complexity of creating new instances. Consider using the stereotype pattern when creating similar objects repeatedly
- Disadvantages: Each class must be equipped with a clone method deep replication is more complex
4. Design pattern usage scenarios and attention
4.1 Application Scenarios
- Copy the structure and data of an object
- You want to make changes to the target object that do not affect existing prototype objects
- The cost of creating an object is high
4.2 pay attention to
- Copying objects using the stereotype pattern does not invoke the class constructor. Therefore, the singleton pattern conflicts with the stereotype pattern, so be careful when using it. (Clone cannot be used in singleton mode)
- You don’t have to use final objects
- The Clone method of Object only copies the basic data types of the Object. It does not copy arrays, container objects, reference objects, etc. This is a shallow copy. If deep copy is to be implemented, arrays, container objects, reference objects, and so on in the stereotype schema must be copied separately.
5. Refer to the article
Total content in HeadFirst design mode and related videos