首页 > 编程语言 > 详细

[Java Pattern] Builder pattern

时间:2020-11-13 16:31:27      阅读:24      评论:0      收藏:0      [点我收藏+]

First, let‘s see the code:

// builder/Contact.java

public class Contact {
    private String firstName;
    private String lastName;
    private String emailAddress;

    public Contact() {
        super()
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(final String emailAddress) {
        this.emailAddress = emailAddress;
    }
}

 

Using it:

    @GetMapping("presidents")
    public List<Contact> getPresidents() {
        List<Contact> contacts = new ArrayList<>();

        Contact contact = new Contact();
        contact.setFirstName("George");
        contact.setLastName("Washington");
        contacts.add(contact);

        contacts.add(new Contact("John", "Adams", null));

        return contacts;
    }

For the highlighted part, there are two ways to create contact object.

One is multi lines:

        Contact contact = new Contact();
        contact.setFirstName("George");
        contact.setLastName("Washington");
        contacts.add(contact);

Another way is single line:

contacts.add(new Contact("John", "Adams", null));

 

The problems are 1. too much line of code when object get complex; 2. The order of params you need to rememeber.

 

Builder pattern:

public class ContactBuilder {
    private String firstName;
    private String lastName;
    private String emailAddress;

    public ContactBuilder setFirstName(final String firstName) {
        this.firstName = firstName;
        return this;
    }

    public ContactBuilder setLastName(final String lastName) {
        this.lastName = lastName;
        return this;
    }

    public ContactBuilder setEmailAddress(final String emailAddress) {
        this.emailAddress = emailAddress;
        return this;
    }

    public Contact buildContact () {
        return new Contact(firstName, lastName, emailAddress);
    }
}

Keep returning ‘this‘ to enable chaining method calls. 

 

Using it:

    @GetMapping("presidents")
    public List<Contact> getPresidents() {
        List<Contact> contacts = new ArrayList<>();

        Contact contact = new Contact();
        contact.setFirstName("George");
        contact.setLastName("Washington");
        contacts.add(contact);

        contacts.add(new Contact("John", "Adams", null));

        // using ContactBuilder
        contacts.add(
                new ContactBuilder()
                        .setFirstName("Thomas")
                        .setLastName("Jefferson")
                        .buildContact()
        )
        return contacts;
    }

 

When to use this pattern:

1. Useful when object creation has many parameters

2. Becomes increasingly more useful when some or all parameters are optional

3. Can make code easier to read because of reduced lines for construction when compared with setters

 

[Java Pattern] Builder pattern

原文:https://www.cnblogs.com/Answer1215/p/13969683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!