Builder pattern is a
creational design pattern used to assemble complex object.he intent of the Builder design pattern is to
separate the construction of a complex object from its representation. By doing
so the same construction process can create different representations.
Lets think we have
to create invoice object ,inside invoice we can have invoice date and
amount.What isf invoice has purchase order attributes associated with it then
we need to add attributes associated with purchase order in invoice class.So
for that either we can extend base invoice class to add new parameters for
purchase order attributes but this leads to creating more number of subclasses
as the new parameters come in future.Another alternative tho this is add
parameters in the same class and add new constructor to create the object.Again
same thing more number of attributes and combination of those
attributes leads to more number of constructors .So solution to this is
Builder pattern..
The Builder design
pattern describes how to solve such problems:
·Encapsulate creating
and assembling the parts of a complex object in separate builder object.
· A class delegates object creation to
a builder object instead of creating the objects directly.
A class can delegate object
creation process to builder.
package com.dp.builder; public interface InvoiceBuilder { Invoice build(); }
package com.dp.builder; import java.sql.Date; import java.util.List; public class Invoice { private String name; private double amount; private Date purchaseOrderDate; private String purchaseOrderNumber; private List<String> items; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public Date getPurchaseOrderDate() { return purchaseOrderDate; } public void setPurchaseOrderDate(Date purchaseOrderDate) { this.purchaseOrderDate = purchaseOrderDate; } public String getPurchaseOrderNumber() { return purchaseOrderNumber; } public void setPurchaseOrderNumber(String purchaseOrderNumber) { this.purchaseOrderNumber = purchaseOrderNumber; } public List<String> getItems() { return items; } public void setItems(List<String> items) { this.items = items; } @Override public String toString() { return "Invoice [name=" + name + ", amount=" + amount + ", purchaseOrderDate=" + purchaseOrderDate + ", purchaseOrderNumber=" + purchaseOrderNumber + ", items=" + items + "]"; } }
package com.dp.builder; import java.sql.Date; import java.util.List; public class InvoiceBuilderImpl implements InvoiceBuilder{ private String name; private double amount; private Date purchaseOrderDate; private String purchaseOrderNumber; private List<String> items; public InvoiceBuilderImpl invoiceName(String name){ this.name = name; return this; } public InvoiceBuilderImpl invoiceAmount(double amount){ this.amount = amount; return this; } public InvoiceBuilderImpl purchaseOrderDate(Date purchaseOrderDate){ this.purchaseOrderDate = purchaseOrderDate; return this; } public InvoiceBuilderImpl invoiceAmount(String purchaseOrderNumber){ this.purchaseOrderNumber = purchaseOrderNumber; return this; } public InvoiceBuilderImpl invoiceItems(List<String> items){ this.items = items; return this; } @Override public Invoice build() { Invoice invoice = new Invoice(); invoice.setName(name); invoice.setAmount(amount); invoice.setPurchaseOrderDate(purchaseOrderDate); invoice.setPurchaseOrderNumber(purchaseOrderNumber); invoice.setItems(items); return invoice; } }
package com.dp.builder; import java.sql.Date; import java.util.Arrays; public class Client { public static void main(String[] args) { //create purchase order invoice Invoice poInvoice = new InvoiceBuilderImpl().invoiceName("poInvocie").invoiceAmount(5000).invoiceItems(Arrays.asList("product1","product2")).purchaseOrderDate(new Date(12, 12, 12)).build(); //create non purchase order invoice Invoice nonPoInvoice = new InvoiceBuilderImpl().invoiceName("nonPoInvoce").invoiceAmount(10000).build(); } }
Comments
Post a Comment