Java implementation of Builder (BUilder) model [00 original]__java

Source: Internet
Author: User
The following examples come from Java and schema.
Suppose there is a system of electronic magazines built in Java that regularly sends electronic magazines to users ' email mailboxes. Users can subscribe through a Web page, or they can end subscriptions through a Web page. When a customer starts a subscription, the system sends an e-mail to welcome it, and when the customer ends the subscription, the system sends an e-mail message saying goodbye. This example is the module that the system is responsible for sending "welcome" and "send-off" messages.
1. Class Diagram

  2. Java Implementation CodePackage cn.edu.ynu.sei.builder;

Import Java.util.Date;
Import java.util.Properties;

Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;

/**
* Mail Message
*
* @author 88250
* @version 1.0.0, 2007-8-24
*/
Public abstract class Automessage
{
protected String subject = "";
protected String BODY = "";
protected String from = "";
protected String to = "";
protected Date senddate = null;

Public Automessage () {}

public void Send ()
{
Your SMTP server address
String smtphost = "smtp.mycompany.com";

Mail address of Sender
String from = "jeff.yan@mycompany.com";

e-mail address of the person receiving the letter
String to = "ni.hao@yourcompany.com";

Properties Props = new properties ();
Props.put ("Mail.smtp.host", SMTPHost);

Session session = Session.getdefaultinstance (props, null);

Try
{
Internetaddress[] Address = {new internetaddress (to)};
MimeMessage message;

Create a Message Object
message = new MimeMessage (session);
Building a sender's address part
Message.setfrom (New InternetAddress (from));
Building Theme Parts
Message.setsubject ("Hello from Jeff");

Build Send Time part
Message.setsentdate (senddate);

Building Content Parts
Message.settext ("Hello, how are things going?");

Send a message, equivalent to a product reception method
Transport.send (message);
System.out.println ("Email has been sent.");
}
catch (Exception e)
{
E.printstacktrace ();
}
}

/**
* @return The subject
*/
Public String Getsubject ()
{
return subject;
}

/**
* @param subject the subject to set
*/
public void Setsubject (String subject)
{
this. Subject = subject;
}

/**
* @return The body
*/
Public String GetBody ()
{
return to body;
}

/**
* @param body to set
*/
public void Setbody (String body)
{
this. Body = body;
}

/**
* @return the From
*/
Public String Getfrom ()
{
return from;
}

/**
* @param from-to set
*/
public void Setfrom (String from)
{
this. From = from;
}

/**
* @return The Senddate
*/
Public Date getsenddate ()
{
return senddate;
}

/**
* @param senddate the senddate to set
*/
public void Setsenddate (Date senddate)
{
this. senddate = Senddate;
}

/**
* @return The To
*/
Public String Getto ()
{
return to;
}

/**
* @param to the To set
*/
public void Setto (String to)
{
this. to = to;
}
}

Package cn.edu.ynu.sei.builder;

Import Java.util.Date;

/**
* Abstract Builder
*
* @author 88250
* @version 1.0.0, 2007-8-24
*/
Public abstract class Builder
{
protected Automessage msg;

Public Builder () {}

/**
* Theme Parts Construction method
*/
public abstract void Buildsubject ();

/**
* Content Part Construction method
*/
public abstract void Buildbody ();

/**
* Sender Part Construction method
* @param from
*/
public void Buildfrom (String from)
{
Msg.setfrom (from);
}

/**
* Recipient Part Construction method
* @param to
*/
public void Buildto (String to)
{
System.out.println (to);
Msg.setto (to);
}

/**
* Send Time Part construction method
*/
public void Buildsenddate ()
{
Msg.setsenddate (New Date ());
}

/**
* Send mail with this method when the mail product is finished <br>
* This method is equivalent to the product return method
*/
public void SendMessage ()
{
Msg.send ();
}
}

Package cn.edu.ynu.sei.builder;

/**
* Mail Sending system customer
*
* @author 88250
* @version 1.0.0, 2007-8-24
*/
public class Client
{
private static Builder Builder;
private static Director Director;

public static void Main (string[] args)
{
Builder = new Welcomebuilder ();

Director = new Director (builder);
Director.construct ("jeffyan77@yahoo.com", "javapatterns@yahoo.com");
}
}

Package cn.edu.ynu.sei.builder;

/**
* Director Role
*
* @author 88250
* @version 1.0.0, 2007-8-24
*/
public class Director
{
Builder Builder;

Public Director (Builder Builder)
{
this. Builder = Builder;
}

/**
* Product Construction method, responsible for the call of the parts construction method
* @param toaddress
* @param fromaddress
<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.