官术网_书友最值得收藏!

  • Spring 5 Design Patterns
  • Dinesh Rajput
  • 319字
  • 2021-07-08 09:59:34

Sample implementation of the Builder design pattern

In the following code example, I am going to create an Account class that has AccountBuilder as an inner class. The AccountBuilder class has a method to create an instance of this class:

    package com.packt.patterninspring.chapter2.builder.pattern; 
    public class Account { 
      private String accountName; 
      private Long accountNumber; 
      private String accountHolder; 
      private double balance; 
      private String type; 
      private double interest; 
      private Account(AccountBuilder accountBuilder) { 
         super(); 
         this.accountName = accountBuilder.accountName; 
         this.accountNumber = accountBuilder.accountNumber; 
         this.accountHolder = accountBuilder.accountHolder; 
         this.balance = accountBuilder.balance; 
         this.type = accountBuilder.type; 
         this.interest = accountBuilder.interest; 
      } 
      //setters and getters 
       public static class AccountBuilder { 
         private final String accountName; 
         private final Long accountNumber; 
         private final String accountHolder; 
         private double balance; 
         private String type; 
         private double interest; 
         public AccountBuilder(String accountName, 
String accountHolder, Long accountNumber) { this.accountName = accountName; this.accountHolder = accountHolder; this.accountNumber = accountNumber; } public AccountBuilder balance(double balance) { this.balance = balance; return this; } public AccountBuilder type(String type) { this.type = type; return this; } public AccountBuilder interest(double interest) { this.interest = interest; return this; } public Account build() { Account user = new Account(this); return user; } } public String toString() { return "Account [accountName=" + accountName + ",
accountNumber=" + accountNumber + ", accountHolder=" + accountHolder + ", balance=" + balance + ", type="
+ type + ", interest=" + interest + "]"; } }

AccountBuilderTest.java is a demo class that we will use to test the design pattern. Let's look at how to build an Account object by passing the initial information to the object:

     package com.packt.patterninspring.chapter2.builder.pattern; 
     public class AccountBuilderTest { 
       public static void main(String[] args) { 
         Account account = new Account.AccountBuilder("Saving
Account", "Dinesh Rajput", 1111l) .balance(38458.32) .interest(4.5) .type("SAVING") .build(); System.out.println(account); } }

You can test this file and see the output on the console:

Now, we've seen the Builder design pattern. In the upcoming Chapter 3, Consideration of Structural and Behavioural Patterns, I will explore another part of the GOF Design Patterns family.

主站蜘蛛池模板: 方山县| 阿克苏市| 彩票| 长岛县| 北京市| 靖西县| 定州市| 台前县| 吉木乃县| 宁津县| 宾川县| 浦东新区| 盐源县| 井冈山市| 兴海县| 吕梁市| 简阳市| 胶南市| 蚌埠市| 安化县| 霍城县| 宿松县| 北流市| 安西县| 韩城市| 屯门区| 都匀市| 呈贡县| 泸定县| 平乐县| 荔浦县| 宜兰市| 禄丰县| 秦安县| 武清区| 泽普县| 蓝田县| 阳信县| 科技| 子长县| 饶河县|