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

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.

主站蜘蛛池模板: 乾安县| 井研县| 师宗县| 广宁县| 晋州市| 桦南县| 甘谷县| 晋江市| 万州区| 安西县| 清涧县| 喀喇| 临安市| 涟源市| 巴里| 元阳县| 莱州市| 翁源县| 龙川县| 怀化市| 南宫市| 彩票| 凤冈县| 莒南县| 吉林省| 客服| 大埔县| 临猗县| 丰原市| 开平市| 昌平区| 渑池县| 油尖旺区| 天峻县| 云安县| 英德市| 武安市| 中方县| 临潭县| 依安县| 丰都县|