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

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.

主站蜘蛛池模板: 溧阳市| 武山县| 秦安县| 勐海县| 本溪市| 曲松县| 苍山县| 海晏县| 资溪县| 雷山县| 闻喜县| 渝北区| 龙州县| 旺苍县| 雅安市| 盐源县| 灵川县| 宾阳县| 桑植县| 桓台县| 日照市| 梅河口市| 肃北| 临泽县| 乐昌市| 怀化市| 武宣县| 金塔县| 浏阳市| 钦州市| 建宁县| 平陆县| 彭山县| 阿拉善左旗| 玉环县| 林甸县| 车险| 玛曲县| 鞍山市| 桑日县| 普兰店市|