builder design pattern

1, the classic mode Builder

Product

/**
 * 计算机抽象类, 即Product角色
 */
public abstract class Computer {

    protected String mBoard;
    protected String mDisplay;
    protected String mOS;

    public Computer() {
    }

    public void setBoard(String board) {
        mBoard = board;
    }

    public void setDisplay(String display) {
        mDisplay = display;
    }

    public abstract void setOS();

    @Override
    public String toString() {
        return "Computer {" 
                + "mBoard = '" 
                + mBoard
                 +' \ '' 
                + ", the mDisplay = '" 
                + the mDisplay
                 +' \ '' 
                + ", mOS = '" 
                + mOS
                 +' \ '' 
                + '}' ; 
    } 
} 
copy the code 

author: mundane 
links: HTTPS: // juejin.im/post/5aa3dfd66fb9a028c42dd13a 
source: Nuggets 
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
View Code
Specific Product
/ ** 
 * Computer specific class 
 * / 
public  class the Macbook the extends Computer { 

    public the Macbook () { 
    } 

    @Override 
    public  void Setos () { 
        mOS = "the Mac the OS X-10.10" ; 
    } 
} 

OF: mundane 
links: HTTPS: // juejin.im/post/5aa3dfd66fb9a028c42dd13a 
source: Nuggets 
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
View Code

Abstract Builder

/ ** 
 * Builder abstract class 
 * / 
public  abstract  class Builder { 

    public  abstract  void buildBoard (String Board); 

    public  abstract  void buildDisplay (String the display); 

    public  abstract  void buildos (); 

    public  abstract Computer Create (); 

} 

OF: mundane 
Links: HTTPS: // juejin.im/post/5aa3dfd66fb9a028c42dd13a 
source: Nuggets 
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

ConcreteBuilder

/**
 * 具体的Builder类, MacbookBuilder
 */
public class MacbookBuilder extends Builder { private Computer mComputer = new Macbook(); @Override public void buildBoard(String board) { mComputer.setBoard(board); } @Override public void buildDisplay(String display) { mComputer.setDisplay(display); } @Override public void buildOS() { mComputer.setOS(); } @Override public Computer create() { return mComputer; } } 复制代码

 


Author: mundane
link: https: //juejin.im/post/5aa3dfd66fb9a028c42dd13a
Source: Nuggets
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
 
/ ** 
 * Director class responsible for building Computer 
 * / 
public  class Director { 

    Builder mBuilder; 

    public Director (Builder Builder) { 
        mBuilder = Builder; 
    } 

    public  void Construct (Board String, String the display) { 
        mBuilder.buildBoard (Board); 
        mBuilder .buildDisplay (Run the display); 
        mBuilder.buildOS (); 
    } 
} 


author: mundane 
links: HTTPS: // juejin.im/post/5aa3dfd66fb9a028c42dd13a 
source: Nuggets 
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
public  class the Main { 

    public  static  void main (String [] args) { 
        Builder Builder = new new MacbookBuilder (); 
        Director pcDirector = new new Director (Builder); 
        pcDirector.construct ( "Intel motherboard", "Retina Display" ); 
        Computer MacBook = Once builder.create (); 
        System.out.println ( "Computer Info:" + macBook.toString ()); 
    } 
} 


author: mundane 
links: HTTPS: // juejin.im/post/5aa3dfd66fb9a028c42dd13a 
source: Nuggets 
copyright of the author all. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Output:

Computer Info: Computer {mBoard = 'Intel motherboard', mDisplay = 'Retina display', mOS = 'Mac OS X 10.10'}

As can be seen, the classic Builder pattern focuses on abstract step object created by calling a different implementation class in order to get different results, but is still in the process of creating multiple parameters to be passed, it is not very convenient, so there a variety of Builder mode

2, a variant of Builder mode

public  class the User { 

    Private  Final String firstName;      // will pass parameters 
    Private  Final String lastName;       // will pass parameters 
    Private  Final  int Age;               // optional parameter 
    Private  Final String Phone;          // optional parameter 
    Private  Final String address;        / / optional parameter 

    Private the User (UserBuilder Builder) {
         the this .firstName = builder.firstName;
         the this .lastName = builder.lastName;
         the this.age = builder.age;
        this.phone = builder.phone;
        this.address = builder.address;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }

    public static class UserBuilder {
        private final String firstName;
        private final String lastName;
        private int age;
        private String phone;
        private String address;

        public UserBuilder(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public UserBuilder age(int age) {
            this.age = age;
            return this;
        }

        public UserBuilder phone(String phone) {
            this.phone = phone;
            return this;
        }

        public UserBuilder address(String address) {
            this.address = address;
            return this;
        }

        public User build() {
            return new User(this);
        }
    }
}
Use 
new new
User.UserBuilder ( "King", "small two" ) .age ( 20 ) .phone ( "123456789" ) .Address ( "Atlantis" ) .build ();

The only possible problem is caused extra Builder objects consume memory. However, in most cases we Builder inner class using a modified static (static), so this problem did not matter much.

Because Builder not thread-safe, so if you want to check a parameter in the Builder category internal legitimacy, will need to check in after the object is created.

public User build() {
  User user = new user(this);
  if (user.getAge() > 120) {
    throw new IllegalStateException(“Age out of range”); // 线程安全
  }
  return user;
}

The above wording is correct, but the code is not thread safe following:

public User build() {
  if (age > 120) {
    throw new IllegalStateException(“Age out of range”); // 非线程安全
  }
  return new User(this);
}

 

Guess you like

Origin www.cnblogs.com/LittleSpring/p/11417463.html