Main application scenarios of the prototype design model:
1. class initialization consumes a lot of resources
2. Using new to generate an object requires tedious data preparation or access permissions,
The following uses sending an advertisement email as an example to describe how to use the prototype design mode.
When an advertising email is sent, except for the title and address of the email, it is the same, so we can extract the unchanged content as a template (AdvTemplate ), then define a mail class and write a constructor. The parameter is the template above. Create a mail according to the template. The Code is as follows:
[Java]
Public class AdvTemplate
{
// Advertisement letter name
Private String advSubject = "Agricultural Bank of China National Day credit card lottery ";
// Ad content
Private String advContext = "Hello, you have been drawn by the system. Please go to the nearby Agricultural Bank of China to get 1 million cash ....";
// Obtain the title of the advertising letter
Public String getAdvSubject ()
{
Return advSubject;
}
// Obtain the content of the advertising letter
Public String getAdvContext ()
{
Return advContext;
}
}
Compile an email class:
[Java]
Public class Mail implements Cloneable
{
Private String Explorer;
Private String subject;
Private String appellation;
Private String context;
Private String tail;
// Constructor
Public Mail (AdvTemplate tem)
{
This. subject = tem. getAdvSubject ();
This. context = tem. getAdvContext ();
}
Public String getReceiver ()
{
Return aggreger;
}
Public void setReceiver (String receiver)
{
This. Cycler = Cycler;
}
Public String getSubject ()
{
Return subject;
}
Public void setSubject (String subject)
{
This. subject = subject;
}
Public String getAppellation ()
{
Return appellation;
}
Public void setAppellation (String appellation)
{
This. appellation = appellation;
}
Public String getContext ()
{
Return context;
}
Public void setContext (String context)
{
This. context = context;
}
Public String getTail ()
{
Return tail;
}
Public void setTail (String tail)
{
This. tail = tail;
}
@ Override
Public Mail clone ()
{
// TODO Auto-generated method stub
Mail mail = null;
Try
{
Mail = (Mail) super. clone ();
} Catch (CloneNotSupportedException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return mail;
}
}
Compile a customer class:
[Java]
Public class Client
{
Private static int MAX_COUNT = 6;
Public static void main (String [] args)
{
Int I = 0;
Mail mail = new Mail (new AdvTemplate ());
Mail. setTail ("ABC copyright ");
While (I <MAX_COUNT)
{
Mail temp = mail. clone ();
Temp. setAppellation (getRandomString (4) + "Mr. (MS )");
Temp. setcycler (getRandomString (4) + "@" + getRandomString (5) + ". com ");
SendMail (temp );
I ++;
}
}
Public static String getRandomString (int len)
{
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
StringBuffer buf = new StringBuffer ();
Random random = new Random ();
For (int I = 0; I <len; I ++)
{
Buf. append (str. charAt (random. nextInt (str. length ())));
}
Return buf. toString ();
}
Public static void sendMail (Mail mail)
{
System. out. println ("title:" + mail. getSubject () + "\ t" + "Recipient:" + mail. getcycler () + "\ t sent successfully... ");
}
}
Author: yuanzeyao2008