• [Spring Pattern] Builder pattern


    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

  • 相关阅读:
    使用sshpass方式实现ssh自动登陆
    MySQL高可用负载均衡
    linux 查看文件夹下的文件个数(当前目录的文件数)
    centos6.5 rsync+inotify实现服务器之间文件实时同步
    Linux shell之数组
    Shell脚本 $0, $#, $*, $@, $?, $$等变量说明
    Linux输出重定向>和>>区别
    EXCEL操作实用学习笔记
    mysql的学习笔记-视图
    mysql学习笔记-索引
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13969683.html
Copyright © 2020-2023  润新知