Four builder patterns for Java design Patterns (builder)

Source: Internet
Author: User

The factory class pattern provides the pattern of creating a single class, while the builder pattern is to centralize the various products to manage, to create composite objects, so-called composite objects that refer to a class that has different properties, in fact the builder pattern is the combination of the previous abstract factory pattern and the last Test. Let's look at the code:

As before, a sender interface, two implementation classes MailSender and Smssender. Finally, the Builder class is as follows:

[Java]View Plaincopy
  1. public class Builder {
  2. Private list<sender> List = new arraylist<sender> ();
  3. public void Producemailsender (int count) {
  4. for (int i=0; i<count; i++) {
  5. List.add (New MailSender ());
  6. }
  7. }
  8. public void Producesmssender (int count) {
  9. for (int i=0; i<count; i++) {
  10. List.add (New Smssender ());
  11. }
  12. }
  13. }

Test class:

[Java]View Plaincopy
    1. public class Test {
    2. public static void Main (string[] args) {
    3. Builder builder = new Builder ();
    4. Builder.producemailsender (10);
    5. }
    6. }

From this point of view, the builder pattern integrates many functions into a class that can create more complex things. So the difference with the engineering model is that the factory model is concerned with creating a single product, while the builder pattern is concerned with creating a conforming object, multiple parts. Therefore, the choice of the factory model or the builder model depends on the actual situation.

Four builder patterns for Java design Patterns (builder)

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.