Java and mode Learning Series-builder Mode

Source: Internet
Author: User

 

I, Construction (builder) mode structure diagram

The builder mode hides the product structure and product part construction process from the client, and separates the responsibility for directing the construction process from the responsibility of specific builder parts, achieve responsibility division and encapsulation.

The construction mode creates all parts one by a director object and a specific builder object to create a complete product object.

The structure is as follows:

II, Application of construction mode in Java

JavamailConstruction mode in

Javamail is a group of j2se extension API class libraries. Designers can use it to easily develop client email software with complete functions.

The message and mimemessage classes in javamail are all degraded construction models. As the client and the director object, mailsender provides the "part" nature of the mimemessage object step by step, that is, from, recipient, subject, text, and so on, and finally obtains the entire product object, that is, the mimemessage object.

The Code is as follows:

Public class mailsender {</P> <p> Private Static mimemessage message; <br/> Public static void main (string ARGs []) {<br/> // your SMTP server address <br/> string smtphost = "smtp.mycompany.com "; <br/> // sender address <br/> string from = "jeff.yan@mycompany.com"; <br/> // recipient address <br/> string to = "ni.hao@youcompany.com "; </P> <p> properties props = new properties (); <br/> props. put ("mail. SMTP. host ", smtphost); <br/> sessio N session = session. getdefaultinsatance (props, null); <br/> try {<br/> internetaddress [] address = {New internetaddress ()}; <br/> // create a message object <br/> message = new mimemessage (session); <br/> // create a sender location metapart <br/> message. setfrom (New internetaddress (from); <br/> // creates a recipient's bitwise part <br/> message. setreipients (message. recipienttype. to, address); <br/> // create a theme part <br/> mesage. setsubject ("hello from Jeff"); <br/> // Build the sending time part <br/> message. setsentdate (new date (); <br/> // create content part <br/> message. settext ("Hello, how are things going? "); <Br/> // send an email, which is equivalent to the product return method <br/> transport. send (Message); <br/> system. out. println ("email has been sent"); <br/>} catch (exception e) {<br/> system. out. println (E); <br/>}< br/>} 

III, Under what circumstances does the builder mode work?

1,
The product objects to be generated have a complex internal structure. Each internal component can be an object or an integral part of an object (that is, a product object.

2,
The attributes of the product objects to be generated are mutually dependent. The construction mode can enforce a step-by-step construction process. Therefore, if one attribute of a product object must be assigned a value after another attribute, using the construction mode is a good design idea.

3,
Other objects in the system will be used during object creation, and the creation process of these product objects is not easy to obtain.

IV, Relationship between construction mode and other modes

Differences between the construction mode and the abstract factory Mode

In the abstract factory model, a complete product object is returned every time a factory object is called, and the client may decide to assemble these products into a larger and more complex product, or not. The construction class is different. It builds a complex product at, and the product assembly process takes place within the builder role. The builder-mode client obtains a complete final product.

In other words, although the abstract factory model and the construction model are both design models, the abstract factory model is at a more specific scale, while the construction model is at a more macro scale. A system may consist of a construction mode and an abstract factory mode. The client indirectly calls another abstract factory mode factory role by calling this construction role. Factory mode returns parts of different product groups, while builder mode assembles them.

Construction mode and Policy Mode

The construction mode is very similar to the policy mode in structure. In fact, the construction mode is a special case of the Policy mode. The difference between the two modes is that they have different intentions. The build mode applies to building new objects for the client at. Although different types of specific builder roles have the same interface, the objects they create may be completely different.

The policy mode aims to provide abstract interfaces for algorithms. In other words, a specific policy class encapsulates an algorithm into an object, and different policy objects provide different implementations for a General Service.

 

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.