Builder Pattern

Jika kita memiliki sebuah class dengan banyak argument pada konstruktornya, kita bisa mempertimbangkan menggunakan Builder Pattern. Sebagai contoh

 

public Employee(string name, string carName, int paySheet, int carPrice, int saving, decimal monthlySaving,
           ITaxPaymentService taxPaymentService)
       {
           _name = name;
           _carName = carName;
           _paySheet = paySheet;
           _carPrice = carPrice;
           _saving = saving;
           _monthlySaving = monthlySaving;
           _taxPaymentService = taxPaymentService;
 
       }

kita bisa membuat sebuah class builder seperti ini.

 

public class EmployeeParam
   {
       
       public string Name { get; private set; }
       public int CarPrice { get; private set; }
       public string CarName { get; private set; }
       public int Saving { get; private set; }
       public decimal MonthySaving { get; private set; }
       public int PaySheet { get; private set; }
 
       public class Builder
       {
           private string _name;
           private int _carPrice;
           private string _carName;
           private decimal _monthySaving;
           private int _paySheet;
           private int _saving;
 
           public Builder WithName(string value)
           {
               _name = value;
               return this;
           }
 
           public Builder WithCarName(string value)
           {
               _carName = value;
               return this;
           }
 
           public Builder WithCarPrice(int value)
           {
               _carPrice = value;
               return this;
           }
 
           public Builder WithPaySheet(int value)
           {
               _paySheet = value;
               return this;
           }
 
           public Builder WithSaving(int value)
           {
               _saving = value;
               return this;
           }
 
           public Builder WithMonthlySaving(int value)
           {
               _monthySaving = value;
               return this;
           }
 
           public EmployeeParam Build()
           {
               return new EmployeeParam
               {
                   Name = _name,
                   CarName = _carName,
                   CarPrice = _carPrice,
                   MonthySaving = _monthySaving,
                   PaySheet = _paySheet,
                   Saving = _saving
               };
           }
       }
   }

 

Sekarang kita bisa memanggil class Employee dengan lebih readable seperti ini.

 

var richManBuilder = new EmployeeParam.Builder()
              .WithName("Rikki")
              .WithCarName("BMW")
              .WithPaySheet(40000000)
              .WithCarPrice(400000000)
              .WithSaving(120000000)
              .WithMonthlySaving(15000000)
              .Build();

 

Employee = new Employee(richManBuilder, _taxPaymentService);

 

 

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s